10 Replies - 468 Views - Last Post: 30 November 2012 - 08:22 AM Rate Topic: -----

#1 omrivol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-November 12

Sending parameter to CMD

Posted 29 November 2012 - 12:37 PM

Hello,
I want to create a button, that will execute a command line code. However, i have a syntax error.

Here is the command:
"C:\Program Files (x86)\blabla\xyz.exe" /b /r /p C:\aaa\bbb.rtp

And here is my c# code:
  private void button1_Click(object sender, EventArgs e)
        {
            string strCmdText;
            strCmdText = @""C:\Program Files (x86)\blabla\xyz.exe" /b /r /p C:\aaa\bbb.rtp";
            System.Diagnostics.Process.Start("CMD.exe", strCmdText);
        }


However, i have a syntax error because of the double quote. Can someone help me?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Sending parameter to CMD

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: Sending parameter to CMD

Posted 29 November 2012 - 12:43 PM

Did you try just using old-fashioned escaping? Something like:
strCmdText = "\"C:\\Program Files (x86)\\blabla\\xyz.exe\" /b /r /p C:\\aaa\\bbb.rtp";


I'm not sure this will actually work in C#, but it should. This tends to work pretty much universally.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Sending parameter to CMD

Posted 29 November 2012 - 12:44 PM

You have to escape special characters first:
http://msdn.microsof...y/h21280bw.aspx

strCmdText = @"\"C:\Program Files (x86)\blabla\xyz.exe\" /b /r /p C:\aaa\bbb.rtp";

Damn! Beat to it by Atli while looking up a reference page to link.

This post has been edited by tlhIn`toq: 29 November 2012 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 omrivol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-November 12

Re: Sending parameter to CMD

Posted 30 November 2012 - 07:31 AM

Thanks.

For some reason, the button doesn't work. It's just open the CMD, but nothing happen.
Lets say i want to check ping for Google. Here is the code. Can someone tell my why the CMD just opened but nothings happen?

        
private void button1_Click(object sender, EventArgs e)
        {
            string strCmdText;
            strCmdText = "ping www.google.com";
            System.Diagnostics.Process.Start("CMD.exe", strCmdText);
        }


Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Sending parameter to CMD

Posted 30 November 2012 - 07:40 AM

Ya got me. I don't do much through CMD.

I would just build ping into my application. Personally I think I'd spend as much time trying to figure out the CMD parameters as just using the .NET ping class, only it would look nicer being within my app than have a CMD window open. Plus it would give me more features to play with like automating it, tracking results and so on.

http://msdn.microsof...ation.ping.aspx

If you decide to keep going with the CMD parameters and work it out, please be sure to post the solution here for the next person in the same situation.
Was This Post Helpful? 1
  • +
  • -

#6 omrivol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-November 12

Re: Sending parameter to CMD

Posted 30 November 2012 - 07:57 AM

Well, Actually the command from my original post doesn't work. It's just opened the CMD and nothing happen. Therefor I've tried to send to the CMD much more simple command such as Ping, but still - the CMD opened and nothing happen.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Sending parameter to CMD

Posted 30 November 2012 - 08:05 AM

A quick google of "C# cmd window parameters" got me to an article that showed this example

ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.Arguments = "/c ping " + machine;
processStartInfo.FileName = "cmd.exe";
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();


It seems arguments should be set as a property not a parameter
Was This Post Helpful? 1
  • +
  • -

#8 omrivol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-November 12

Re: Sending parameter to CMD

Posted 30 November 2012 - 08:18 AM

Thanks. Now it's working properly :-)

Do you know what the "/c" means?
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Sending parameter to CMD

Posted 30 November 2012 - 08:20 AM

"/c Carries out the command specified by the string and then terminates"
Was This Post Helpful? 0
  • +
  • -

#10 omrivol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-November 12

Re: Sending parameter to CMD

Posted 30 November 2012 - 08:20 AM

View Postomrivol, on 30 November 2012 - 08:18 AM, said:

Do you know what the "/c" means?


I already found it:
/C Carries out the command specified by string and then terminates
Was This Post Helpful? 0
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Sending parameter to CMD

Posted 30 November 2012 - 08:22 AM

/c : Carries out the command specified by string and then stops.

You could have just executed your program directly, rather than executing the command shell to execute your program.

Oh, and here is a list of command line arguments for cmd.exe
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1