1

I want to have my program open word and then a specific document, really just using word as an example. This could be useful in other situations, but I'd like to know how to do so with arguments. I have a bit of code to open a program, and some code to display an error message if the file path does not exist here:

    private void StartProcess(string path)
    {
        ProcessStartInfo StartInformation = new ProcessStartInfo();

        StartInformation.FileName = path;

        Process process = Process.Start(StartInformation);

        process.EnableRaisingEvents = true;
    }
    private void ClickFunc(object sender, RoutedEventArgs e)
    {
        if (File.Exists(ProgramPath))
        {
            StartProcess(ProgramPath);
        }
        else
        {
            MessageBox.Show("Specified path does not exist, please try again.", "Bad File Path Error", MessageBoxButton.OK);
        }
    }

And I was wondering how I could add arguments to it. Thanks!

Anthony Smyth
  • 305
  • 4
  • 14
  • If you specify a non-executable file (like a Word document), Process.Start will result in the file being opened by the default program for that file's file extension (.doc or .docx). I haven't tested it, but I highly doubt you can pass arguments that way. On the other hand, if you know the path to the Word executable, you could use that in Process.Start rather than the file, and then you could pass arguments, assuming Word pays any attention to command-line arguments, which I don't know if it does. – adv12 Jun 19 '15 at 14:54
  • 1
    http://stackoverflow.com/questions/3268022/process-start-arguments – user666 Jun 19 '15 at 14:57
  • http://stackoverflow.com/questions/15061854/how-to-pass-multiples-arguments-in-processstartinfo – Alan Jun 19 '15 at 15:05
  • Thanks @Zero for the link. I was thinking of something along those lines, but Rami got it covered in the answers section. Thanks again – Anthony Smyth Jun 19 '15 at 15:23
  • @ user666 that article was helpful as well, so thank you. – Anthony Smyth Jun 19 '15 at 18:10

1 Answers1

2

You can add command line arguments using the Arguments property of the ProcessStartInfo class.

Something like this:

 ProcessStartInfo startInfo = new ProcessStartInfo("winword");
 startInfo.Arguments = "/a /b";

Also you can specify the command line arguments as a string in the ProcessStartInfo constructor.

Check this link from MSDN

https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments(v=vs.110).aspx

Rami
  • 7,162
  • 1
  • 22
  • 19