Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Running a program with parameters

  1. #1

    Thread Starter
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Resolved [RESOLVED] [2005] Running a program with parameters

    I'm trying to do the following:

    VB Code:
    1. Dim programparameters as string
    2. openfiledialog.ShowOpen
    3. Process.Start(openfiledialog.Filename & " " & programparameters)

    but, as you would've guessed by now (hence why I am here) is that my program doesn't like it.

    I've ommited the arguments, but further on in the code, I use Shell() command for running a program with arguments.

    Is the Shell command the way to go, or is there a .NET way of doing what I've asked?

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Running a program with parameters

    You can use the Process class in place of the Shell() command, like you were trying to do. The Process class has a StartInfo property where you can specify a StartInfo object, which gives you options to specify the filename and additional arguments to run. An example is below:
    VB Code:
    1. Dim MyProcess As New Process
    2.         Dim MyStartInfo As New ProcessStartInfo
    3.         'filename of the program to run, best to use full path
    4.         MyStartInfo.FileName = "notepad.exe"
    5.         'arguments to pass into the program
    6.         MyStartInfo.Arguments = "my arguments"
    7.         'sets new startinfo object to MyProcess' StartInfo property
    8.         MyProcess.StartInfo = MyStartInfo
    9.         'starts the process
    10.         MyProcess.Start()
    Last edited by gigemboy; Aug 6th, 2006 at 01:19 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Running a program with parameters

    Process.Start is overloaded and you should read the documentation to see how each overload works. If all you want to do is pass some commandline parameters to a new executable then the easiest way is:
    VB Code:
    1. Process.Start("file path", "arguments")
    There are other alternatives too. Gig's is more complex than necessary because you don't have to create both Process and ProcessStartInfo objects. You would normally create either one or the other. That's because a Process object already has a ProcessStartInfo object associated with it, and if you start a process with a ProcessStartInfo object then a Process object is created:
    VB Code:
    1. 'Create the ProcessStartInfo.
    2. Dim psi1 As New ProcessStartInfo
    3.  
    4. 'Set the properties.
    5. psi1.FileName = filePath
    6. psi1.Arguments = arguments
    7.  
    8. 'Start the process and create the Process object.
    9. Dim proc1 As Process = Process.Start(psi1)
    VB Code:
    1. 'Create the ProcessStartInfo with the desired parameters.
    2. Dim psi2 As New ProcessStartInfo(filePath, arguments)
    3.  
    4. 'Start the process and create the Process object.
    5. Dim proc2 As Process = Process.Start(psi2)
    VB Code:
    1. 'Create the new process object.
    2. Dim proc3 As New Process
    3.  
    4. 'Set the properties.
    5. proc3.StartInfo.FileName = filePath
    6. proc3.StartInfo.Arguments = arguments
    7.  
    8. 'Start the process.
    9. proc3.Start()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Re: [2005] Running a program with parameters

    Oh thanks you two. I used gigemboy's version, but I think i'll use jmcilhinney's simplified version instead. Thanks for the help!

  5. #5
    Lively Member rguifarro's Avatar
    Join Date
    Aug 2006
    Location
    Central America
    Posts
    73

    Re: [RESOLVED] [2005] Running a program with parameters

    jmcilhinney's

    in all the forum there is an example on how to process.start and passing comandlines to an X application but how would it be to do it with the cmd.exe? How can I pass comandlines to the cmd window? Already tried many things but can't get the comandlines to be writen on the cmd window.

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