How to execute exe via code?

Hello. What you are attempting to do is very simple.

VB.NET:
Shell([COLOR="red"]{filename}[/COLOR], [COLOR="green"]{appwinstyle}[/COLOR], [COLOR="blue"]{wait}[/COLOR], [COLOR="purple"]{timeout}[/COLOR])

There are four options, but only the FileName is required, the rest are optional.

{filename} : The full path and filename of the file you are wanting to execute (eg. C:\abc.exe)

{appwinstyle} : The window style for the loaded exe (maximized, minimized, normal, focused, not focused)

{wait} : Is either True or False. If True, makes your program wait for the loaded program to exit before continuing code.

{timeout} : Is the time (in milliseconds) in which your program will wait for the program to start before eventually giving up and continuing the code.

Hope this helps.
 
I wouldn't use Shell() at all, I'd use the .Net way:

Process.Start("exe file path & name")
Process.Start("exe file path & name", "Args")
 
Ah, yes. Sounds good! More "modern" way. :)
One of the many advantages to using the process class is that you can hang on to the reference to the process when executing it:

Private SomePrc As Process

'Button click event or something:
SomePrc = Process.Start("exe file", "optional args")

'Some other event:
If SomePrc IsNot Nothing Then .... Code related to that process, which is still running

.. Just a quick example, you can so that with Shell(), but the problem is you have to enumerate the list of running processes, and if that exe was ran outside of our program then which one was our program? That's why I use Process.Start
 
VB.NET:
    Private Function WriteRegistry(ByVal StartWithWindows As Boolean) As Boolean
        Dim Output As Boolean = False
        Dim AppName As String = System.IO.Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs(0I))
        Dim SubKey As Microsoft.Win32.RegistryKey = Nothing
        Try
            SubKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree)
            If StartWithWindows Then
                'Create
                SubKey.SetValue(AppName, Environment.GetCommandLineArgs(0I))
            Else
                'Delete
                Dim KeyExists As Boolean = False
                For Each strkey As String In SubKey.GetValueNames
                    If strkey = AppName Then KeyExists = True
                Next strkey
                If KeyExists Then SubKey.DeleteValue(AppName)
            End If
            Output = True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "WriteRegistry")
        Finally
            If SubKey IsNot Nothing Then SubKey.Close()
        End Try
        Return Output
    End Function
That will toggle the key in the current user's run subkey in the registry, just pass it a "true" if you want it to start with windows for that user, and "false" if you dont want it to start with that user

If you want a little more detail/refinement than that, see my other thread: Start with windows, registry writing - VBForums
 
JuggaloBrotha said:
Finally
My.Computer.Registry.CurrentUser.Close()
This must be a misconception. CurrentUser is a base key (system key), Close has no effect on this as system keys are never closed. Close method is also not a cascading call, no other open child keys is closed when you close a key. It is the RegistryKey object returned by OpenSubKey that you must close.
 
This must be a misconception. CurrentUser is a base key (system key), Close has no effect on this as system keys are never closed. Close method is also not a cascading call, no other open child keys is closed when you close a key. It is the RegistryKey object returned by OpenSubKey that you must close.
Right, I've changed the code in my post.
 
Back
Top