CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2005
    Posts
    42

    Question Opening an .exe file

    How can I open another .exe file from my vb.net application?

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Opening an .exe file

    If you want to execute another EXE from within your VB.NET App, then you could use
    Code:
    'Put this on top of the of the Code
    Imports System.Diagnostics
    
    'Put this at a place where you want to execute the Exe
            Dim newProc As Process = New Process
            newProc.Start("C:\another.EXE")

  3. #3
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Opening an .exe file

    In VB.NET you have the Shell function.
    For example (from MSDN)
    Code:
    Dim ProcID As Integer
    ' Start the Calculator application, and store the process id.
    ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
    ' Activate the Calculator application.
    AppActivate(ProcID)
    SendKeys.SendWait("22")
    SendKeys.SendWait("*")
    SendKeys.SendWait("44")
    SendKeys.SendWait("=")
    It is found in Microdsoft.VisualBasic namespace.

  4. #4
    Join Date
    Nov 2004
    Location
    LA. California Raiders #1 AKA: Gangsta Yoda™
    Posts
    616

    Re: Opening an .exe file

    Shell is part of the old Microsoft.VisualBasic namespace. Its not considered as .NET code since MS just added
    some of the VB6 functions to help make the transition easier for vb6'ers.

    Using the System.Diagnostics.Process namespace is the best solution.
    VB/Office Guru™ (AKA: Gangsta Yoda™)
    VB Forums - Super Moderator 2001-Present

    Microsoft MVP 2006-2011

    Please use [code]your code goes in here[/code] tags when posting code.

    Senior Software Engineer MCP, BSEE, CET
    VS 2012 Premium, VS 6.0 Enterprise SP6, VSTO, Office Ultimate 2010, Windows 7 Ultimate
    Star Wars Gangsta Rap SE Reputations & Rating Posts Office Primary Interop AssembliesAdvanced VB/Office Guru™ Word SpellChecker™.NETAdvanced VB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address ListVB6/Crystal Report Ex.VB6/CR Print Setup Dialog Ex.

  5. #5
    Join Date
    Apr 2005
    Posts
    42

    Unhappy Re: Opening an .exe file

    I am unable to start another exe by implementing both of above specified methods. I get the error of 'File nt Found' though the specified exe is present in directory C:\. I am unable to handle this exception though these methods work very fine if the specified file is 'CALC.EXE' or 'CMD.EXE' but the same code does not work if the specified file is any other executable e.g. "C:\TCPServer.exe", i get the following error, plzzzz help !

    Unhandled Exception: System.IO.FileNotFoundException: File not found.
    at Microsoft.VisualBasic.Interaction.Shell(String Pathname, AppWinStyle Style , Boolean Wait, Int32 Timeout) at newProcess.Module1.Main() in :\Documents and Settings\Tess\My Documents\Visual Studio Projects\newProcess\Module1.vb:line 10
    Press any key to continue

  6. #6
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Opening an .exe file

    You have two options:
    1. Pass as an arguement the full path to the executable you want to run.
    2. Add to the path environment variable the path to the executable.

    I prefer the first solution, and you can also store the full path in a configuration file.

  7. #7
    Join Date
    Apr 2005
    Posts
    42

    Unhappy Re: Opening an .exe file

    Giving the whole path of the executable as an argument is not working as i explained before about the unhandled exception i am receiving. About the 2nd option, can you plz guide me how do we add it as environment variable. Please Help, thankyou.

  8. #8
    Join Date
    Jan 2005
    Posts
    70

    Re: Opening an .exe file

    Are you escaping the \ in the filepath ? Or putting an @ at the start of the string that houses the path?

  9. #9
    Join Date
    Apr 2005
    Posts
    42

    Re: Opening an .exe file

    I am not escaping or adding such characters.

  10. #10
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Opening an .exe file

    In order to add the path to the Path environment variable:
    Right click on My Computer, and select Properties.
    Go to "Advanced" tab, and click on "Environment Variable".
    On the top list find the Path variable and click on it.
    Add the path(only folder) to the end of the path (seperating with ; char)
    Click Ok twice.

    You will need to close all instances of Visual Studio for the changes to take affect.

    When you call from code simply give the executable name (without full path).

    If this still doesn't work, remove from path, and try the bottom list of Environment Variable (the System ones).

    If this still doesn't work, I strongly suggest you check if you spell the executable correctly, and the full path, and that the file is actually there, and remove any file properties (like readonly).

    Hope this helps.

  11. #11
    Join Date
    Apr 2005
    Posts
    42

    Unhappy Re: Opening an .exe file

    Quote Originally Posted by vb_the_best
    If you want to execute another EXE from within your VB.NET App, then you could use
    Code:
    'Put this on top of the of the Code
    Imports System.Diagnostics
    
    'Put this at a place where you want to execute the Exe
            Dim newProc As Process = New Process
            newProc.Start("C:\another.EXE")
    This code does not work..and gives the error file not found,although the file is there.

  12. #12
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Opening an .exe file

    Quote Originally Posted by SoftLock
    This code does not work
    It worked on my computer on an exe located on C:\ .
    There is nothing wrong with the code vb_the_best gave.

    .NET can't find your exe.

  13. #13
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Opening an .exe file

    Quote Originally Posted by SoftLock
    This code does not work..and gives the error file not found,although the file is there.
    As jhammer has already posted, it works perfectly on my system too...
    Can you post the code you wrote, and make sure that the file you are trying o execute is present at the location you have given...

  14. #14
    Join Date
    Jul 2005
    Posts
    1

    Cool Re: Opening an .exe file

    test kirim file vb code

  15. #15
    Join Date
    Apr 2005
    Posts
    42

    Smile Re: Opening an .exe file

    thankyou all..infact the exe i was trying to open was named as "TCPServer.exe", and i just renamed the same exe and it got opened. the code worked perfectly fine. this was strange with that specific exe...neways, problem got solved, thnkyou all again for ur interest.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured