0

I'm trying to start a local instance of notepad with a text file to try out c# cmd line arguments for eventual use in a remote connection script. I'm using System.Diagnostics.Process, but the StartInfo.Arguments doesn't actually run completely and open the notepad instance.

            var p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = @"cd\ start notepad C:\test\testcmdline.txt";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

The window opens at root, which tells me the cd\ is working, but the "start notepad" does not seem to be running.

Am I missing something about the structure of StartInfo.Arguments?

EDIT: I'm trying to figure out how to run a python script on a remote server, and using this as a test for running things in cmd in c#. While it's fine to run this in notepad, I'm not sure if the principle would carry over to the eventual implementation of running a python script remotely so I'm attempting to learn how to run items through cmd in C# in general.

Jaeriko
  • 31
  • 10
  • 1
    Do you really need to execute multiple statements? Couldn't you simply use `notepad.exe` as FileName and use `@"C:\test\testcmdline.txt"` as Arguments? – bassfader Sep 25 '17 at 15:12
  • That does work, but I'm using this as a template for how I'll be running a python script on a remote server through cmd line after connecting. Running on the cmd line seems like it would be a decent point of commonality between the two. – Jaeriko Sep 25 '17 at 15:16
  • 1
    Then, AFAIK, you'll need to set `RedirectStandardInput` of the process to `true`, and then create a `StreamWriter` for writing your individual commands to the `Process.StandardInput` stream. Take a look at the answer with the most upvotes in the following link: https://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net – bassfader Sep 25 '17 at 15:19
  • Use && to separate multiple commands. – Hans Passant Sep 25 '17 at 15:33

2 Answers2

0

I ended up using the more simple 2 arg Process.Start.

string cmdText;
cmdText = @"/C C:\test\testcmdline.txt";
Process.Start("cmd.exe", cmdText);
Jaeriko
  • 31
  • 10
0

Try adding /c in the beginning of the Arguments.
Or the above task can be done as below

var process = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                Arguments = @"/c start notepad C:\test\testcmdline.txt"
            };
            Process.Start(process );
Sarang M K
  • 261
  • 3
  • 9