Last active
March 31, 2023 03:00
-
-
Save Carm01/b4a4ea51deda8163a19c63165ee7f70d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Right click copy to clipboard | |
Private Sub ContextAdditionalDays_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextAdditionalDays.Opening | |
Try | |
Clipboard.SetText(lblAdditionalDays.Text)' Copy to clipboard | |
Catch ex As Exception | |
' | |
End Try | |
End Sub | |
'================================================================================================= | |
'Onload event to prevent application from running Twice | |
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load | |
'Check to prevent running twice | |
'https://stackoverflow.com/questions/1328792/make-single-instance-application-what-does-this-do | |
Dim _process() As Process | |
_process = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName) | |
If _process.Length > 1 Then | |
MsgBox("Another instance is already running!", vbInformation) | |
End | |
End If | |
'If you get to this point it's first instance | |
End Sub | |
'================================================================================================= | |
'Minimize Window | |
Me.WindowState = FormWindowState.Minimized | |
'================================================================================================= | |
'The Followning ITems are examples on how to allow a boarderless form to be moved. The variables need to be in the class level. Select Items on the form which you want to be able to use to grab and move the form | |
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown | |
mouseDwn = True | |
mousex = MousePosition.X - Me.Left | |
mousey = MousePosition.Y - Me.Top | |
End Sub | |
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove | |
If mouseDwn Then | |
Me.Top = MousePosition.Y - mousey | |
Me.Left = MousePosition.X - mousex | |
End If | |
End Sub | |
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp | |
mouseDwn = False | |
End Sub | |
'================================================================================================= | |
'Some Date/Time picker stuff | |
'grabs dates from pickers | |
startDate = DateTimePicker1.Text | |
endDate = DateTimePicker2.Text | |
'Calculates date difference | |
Dim duration As TimeSpan = endDate - startDate | |
'Isolates Days difference | |
Dim diff As Integer = duration.TotalDays | |
'Isolates Day of week start date | |
Dim CheckDay = startDate.DayOfWeek | |
'================================================================================================= | |
'Procedure example of check for running process and kill it without showing window | |
Private Sub CheckIfRunning(xProcess)' example of xProcess = 'regedit' no ".exe" is needed. | |
p = Process.GetProcessesByName(xProcess) | |
If p.Count > 0 Then | |
Shell("Taskkill.exe /im " & xProcess & ".exe /f", AppWinStyle.Hide) | |
Else | |
'MsgBox("Process not running") | |
End If | |
End Sub | |
'================================================================================================= | |
'Example of properly reading/writing to the HKLM registry on a 64Bit machine | |
Dim KeySub As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer" | |
Dim KeyValue As String = "DisableEdgeDesktopShortcutCreation" | |
Dim TS__x64 As Microsoft.Win32.RegistryKey = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry64) | |
'Read HKLM registry value | |
Dim CALE_TS__x64 As String = TS__x64.OpenSubKey(KeySub).GetValue(KeyValue) | |
MsgBox(CALE_TS__x64) | |
'Write value HKLM | |
TS__x64.OpenSubKey(KeySub, True).SetValue(KeyValue, 1, Microsoft.Win32.RegistryValueKind.DWord) | |
CALE_TS__x64 = TS__x64.OpenSubKey(KeySub).GetValue(KeyValue) | |
MsgBox(CALE_TS__x64) | |
'================================================================================================= | |
'Opeing a user HIVE ; writing and reading values | |
testReg = My.Computer.Registry.GetValue("HKEY_USERS\TempHive\Environment", "Temp", Nothing) | |
If testReg <> "" Then | |
Do | |
Threading.Thread.Sleep(800) | |
Shell("reg unload hku\TempHive", AppWinStyle.Hide) | |
testReg = My.Computer.Registry.GetValue("HKEY_USERS\TempHive\Environment", "Temp", Nothing) | |
Loop Until testReg = Nothing Or testReg = "" | |
End If | |
Threading.Thread.Sleep(800) | |
Shell("reg.exe load HKU\TempHive C:\Users\Default\NTUSER.dat", AppWinStyle.Hide) | |
Threading.Thread.Sleep(800) | |
testReg = My.Computer.Registry.GetValue("HKEY_USERS\TempHive\Environment", "Temp", Nothing) | |
If Not testReg = "" Then | |
My.Computer.Registry.SetValue("HKEY_USERS\TempHive\Software\Microsoft\Office\16.0\Common\FileIO", | |
"DisableNotificationIcon", "1", Microsoft.Win32.RegistryValueKind.DWord) | |
Getreg = My.Computer.Registry.GetValue("HKEY_USERS\TempHive\Software\Microsoft\Office\16.0\Common\FileIO", "DisableNotificationIcon", Nothing) | |
MsgBox(Getreg, 0, "Read Value") | |
Do | |
Threading.Thread.Sleep(800) | |
Shell("reg unload hku\TempHive", AppWinStyle.Hide) | |
testReg = My.Computer.Registry.GetValue("HKEY_USERS\TempHive\Environment", "Temp", Nothing) | |
Loop Until testReg = Nothing | |
Else | |
' | |
End If | |
'================================================================================================= | |
'Checks for a path to see if it exists | |
If Not My.Computer.FileSystem.DirectoryExists(dblFirefoxPath) Then | |
' | |
End If | |
'================================================================================================= | |
' Downloads a files | |
'(used in Firefox installer) | |
DownloadFile("https://download.mozilla.org/?product=firefox-nightly-latest-ssl&os=win64&lang=en-US", My.Computer.FileSystem.SpecialDirectories.Temp & "\FFInstall.exe") | |
Public Sub DownloadFile(ByVal _URL As String, ByVal _SaveAs As String) | |
Try | |
Dim _WebClient As New System.Net.WebClient() | |
' Downloads the resource with the specified URI to a local file. | |
_WebClient.DownloadFile(_URL, _SaveAs) | |
Catch _Exception As Exception | |
' Error | |
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()) | |
End Try | |
End Sub | |
'================================================================================================= | |
'creates folder | |
My.Computer.FileSystem.CreateDirectory(FFilePath & "distribution") | |
'================================================================================================= | |
'makes sure path exists | |
Do | |
Threading.Thread.Sleep(100) | |
Loop Until My.Computer.FileSystem.DirectoryExists(FFilePath) And My.Computer.FileSystem.DirectoryExists(FFilePath & "distribution") | |
'================================================================================================= | |
'Gets the script directory path; location of exe you are running | |
'gets the path to the exe file running | |
Dim strPath As String = My.Application.Info.DirectoryPath | |
'================================================================================================= | |
'Copies files from your embeded resources to wherever. Last part os a true/false if it appends(True) the file or overwrites(Fales) | |
If My.Computer.FileSystem.FileExists(strPath & "\distribution.ini") = True Then | |
My.Computer.FileSystem.CopyFile(strPath & "\distribution.ini", FFilePath & "distribution\distribution.ini", overwrite:=True) | |
Else | |
My.Computer.FileSystem.WriteAllText(FFilePath & "distribution\distribution.ini", My.Resources.distribution, False) | |
End If | |
'================================================================================================= | |
'creates and writes to files from scratch | |
Dim path As String = FFilePath & "browser\override.ini" | |
Dim fs As FileStream = File.Create(path) | |
Dim info As Byte() = New UTF8Encoding(True).GetBytes("[XRE]" & vbCrLf & "EnableProfileMigrator=false") | |
fs.Write(info, 0, info.Length) | |
fs.Close() | |
path = FFilePath & "defaults\pref\local-settings.js" | |
fs = File.Create(path) | |
info = New UTF8Encoding(True).GetBytes("pref(""general.config.obscure_value"", 0);" & vbCrLf & "pref(""general.config.filename"", ""mozilla.cfg"");") | |
fs.Write(info, 0, info.Length) | |
fs.Close() | |
'================================================================================================= | |
'Delete folder and subfolder | |
My.Computer.FileSystem.DeleteDirectory(FFilePath, FileIO.DeleteDirectoryOption.DeleteAllContents) | |
'================================================================================================= | |
'Example of running an application that already has a command line switch that is silent and waits for it to finish | |
Dim p As New Process | |
p = Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "\FFInstall.exe", " -ms") | |
p.WaitForExit() | |
'An example using Shell to run a command line silently hiding the window | |
Shell(FFilePath & "uninstall\helper.exe /S", AppWinStyle.Hide) | |
'Will have to check for process to not exist anymore before continuing | |
'Another example with wait for exit command added | |
MyUtilities.RunCommandCom(FFilePath & "uninstall\helper.exe", " /S", False) | |
Public Class MyUtilities | |
Shared Sub RunCommandCom(command As String, arguments As String, permanent As Boolean) | |
Dim p As Process = New Process() | |
Dim pi As ProcessStartInfo = New ProcessStartInfo() | |
pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments | |
pi.FileName = "cmd.exe" | |
Dim ShowWindow As Boolean = False | |
pi.CreateNoWindow = Not ShowWindow | |
pi.WindowStyle = ProcessWindowStyle.Hidden | |
p.StartInfo = pi | |
p.Start() | |
p.WaitForExit() | |
End Sub | |
End Class | |
'I know it's an old thread, but... | |
'How about using the Windows Script Host's Run method? It has a bWaitOnReturn parameter. | |
'object.Run (strCommand, [intWindowStyle], [bWaitOnReturn]) | |
Set oShell = CreateObject("WSCript.shell") | |
oShell.run "cmd /C " & App.Path & sCommand, 0, True | |
'intWindowStyle = 0, so cmd will be hidden | |
'also try: | |
'https://stackoverflow.com/questions/22913660/vb-net-wait-a-dos-shell-program-to-terminate-before-continuing-doesnt-work | |
Shell("OpenSees.exe <arguments>",, True) | |
'================================================================================================= | |
'Get Output of MS DOS command | |
'https://www.codemag.com/article/0403051/Managing-Processes-in-.NET | |
Dim p As New Process | |
Dim info As New ProcessStartInfo | |
info.UseShellExecute = False | |
info.CreateNoWindow = True | |
info.FileName = "cmd.exe" | |
info.Arguments = "/c dir *.*" | |
info.RedirectStandardOutput = True | |
p.StartInfo = info | |
p.Start() | |
p.WaitForExit() | |
MsgBox (p.StandardOutput.ReadToEnd()) | |
'================================================================================================= |
Thanks ! I had to fix tho ..
"HKU
to
"HKEY_CURRENT_USER\
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for your work.