8 Replies - 904 Views - Last Post: 26 September 2013 - 12:53 PM Rate Topic: -----

#1 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 80
  • Joined: 28-June 10

Argument not being passed to Process.Start

Posted 25 September 2013 - 11:47 AM

Im putting together a small program to help me with a task that just got dumped on me at work. All it does is call systeminfo to pull some info about a remote computer. the cmd.exe process starts, but my arguments are not passed to it - am I missing something? I can manually type in the contents of strCmdText into the command window and it works just fine.

txtHostname is just a textbox where I can paste in a hostname. I'm changing the window title just to verify the string is being built correctly.

string strCmdText;
strCmdText = "systeminfo /s " + txtHostname.Text;
System.Diagnostics.Process.Start("cmd.exe", strCmdText);
this.Text = strCmdText;


This post has been edited by AdoTheLimey: 25 September 2013 - 11:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Argument not being passed to Process.Start

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,838
  • Joined: 12-December 12

Re: Argument not being passed to Process.Start

Posted 25 September 2013 - 11:58 AM

What is a typical txtHostname? Does it contain odd characters, such as backslashes?

If this string contains backslashes then they will need to be escaped, using another backslash '\\'. You can use Replace() or a verbatim string (with the @-sign):

var pathA = "c:\\somewhere";
var pathB = @"c:\somewhere";

This post has been edited by andrewsw: 25 September 2013 - 12:00 PM

Was This Post Helpful? 1
  • +
  • -

#3 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 80
  • Joined: 28-June 10

Re: Argument not being passed to Process.Start

Posted 25 September 2013 - 11:59 AM

View Postandrewsw, on 25 September 2013 - 11:58 AM, said:

What is a typical txtHostname? Does it contain odd characters, such as backslashes?


alphanumeric, the only special character is a hyphen. I tried escaping the string, just in case, but no change.

I've tried the following too, same result

string strCmdText;
strCmdText = @"systeminfo /s " + txtHostname.Text + @" /u [redacted] /p [redacted]";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe", strCmdText);
psi.WorkingDirectory = "C:\\Windows\\System32";
System.Diagnostics.Process.Start(psi);
this.Text = strCmdText;


This post has been edited by AdoTheLimey: 25 September 2013 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,838
  • Joined: 12-December 12

Re: Argument not being passed to Process.Start

Posted 25 September 2013 - 12:42 PM

Not sure, sorry. Trying embedding a pair of quotes around the hostname:

strCmdText = @"systeminfo /s """ + txtHostname.Text + @""" /u [redacted] /p [redacted]";

This shouldn't be necessary unless the host-name were to contain spaces, but it's worth a shot.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Argument not being passed to Process.Start

Posted 25 September 2013 - 01:03 PM

Why are you trying to run systeminfo.exe through cmd.exe in your Process.Start() call? Just start the process directly.

If you want to go through cmd.exe, you need to use "/c" or "/s /c" as the first few parameters. (Run'cmd /?' from within a CMD window to for more details.)
Was This Post Helpful? 2
  • +
  • -

#6 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 80
  • Joined: 28-June 10

Re: Argument not being passed to Process.Start

Posted 25 September 2013 - 01:24 PM

I did try starting the systeminfo process directly, which worked, but I couldnt figure out how to keep the console window open when it finished.

I have got it working though- I needed to add /k. Thanks for the guidance, fellas!

Updated code
string strCmdText;
strCmdText = @"/k systeminfo /s " + txtHostname.Text + @" /u [redacted] /p [redacted]";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.Arguments = strCmdText;
psi.WorkingDirectory = "C:\\Windows\\System32";
System.Diagnostics.Process.Start(psi);
this.Text = strCmdText;


This post has been edited by AdoTheLimey: 25 September 2013 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 80
  • Joined: 28-June 10

Re: Argument not being passed to Process.Start

Posted 25 September 2013 - 08:15 PM

This little tool saved me a ton of time today. If I wanted to make it a little more sophisticated, like calling systeminfo in the background and making the results available to my code, what libraries should I look at?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Argument not being passed to Process.Start

Posted 25 September 2013 - 09:21 PM

You could redirect the standard output: http://msdn.microsof...dardoutput.aspx

Or you could make the same WMI API calls that the systeminfo does.
Was This Post Helpful? 1
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Argument not being passed to Process.Start

Posted 26 September 2013 - 12:53 PM

Hey everyone, I split the discussion posts out into its own thread in the Advanced Discussion forum, because it was definitely getting off topic. Great discussion though. If you want to keep it going, here's the link:

http://www.dreaminco...art-discussion/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1