C # invokes the command line with a space on the parameter

Source: Internet
Author: User

Call the cmd command in the program to open a file, and the file path with a space, if the path directly to CMD, then CMD will be the path before the space in front of the part as a parameter, the space after the other argument, the command line execution is truncated behind, resulting in a program error, will pop up C : \program is not an internal or external command, and is not an error prompt for a running program or batch file. The workaround is to add double quotes before and after the incoming arguments, as follows:

  PrivateStaticvoidResxtores (ArrayList resxpath) {//Resxfile is a folder that is used to store. resx files that need to be convertedstring s =""; Process p =NewProcess (); p.StartInfo.FileName ="Cmd.exe"; P.startinfo.useshellexecute =False; P.startinfo.redirectstandardinput =True; P.startinfo.redirectstandardoutput =True; P.startinfo.createnowindow =False; P.start ();//+ ' + ' + ' + ' can be replaced with ' \ '//P.standardinput.writeline ("%comspec%/k" + ' + ' + ' + ' "+ @" C:\Program Files (x86) \microsoft Sdks\windows\v7.0a\bin "+" "+ ‘"‘);//P.standardinput.writeline ("%comspec%/k" + ' + ' + ' "' + @" C:\Program Files\Microsoft Visual Studio 10.0\vc\vcvarsall.bat " + ' + ' + ' + ' + ' x86 ');//P.standardinput.writeline (@ "CD C:\Program Files\Microsoft Sdks\windows\v6.0a\bin");for (int i =0; i < Resxpath.count; i++) {String Filename =Resxpath[i]. ToString ();//Write the corresponding algorithm to generate the file name;////resgen E:\Filename1.resx e:\ResName1.resources//P.standardinput.writeline ("ResGen" + filename + "" + System.IO.Path.GetFileNameWithoutExtension (filename) + @ ". resx") ; \stringString Resname =path.getfullpath (Filename) +". resx";String str ="\""+@"C:\Program Files\Microsoft Sdks\windows\v6.0a\bin\resgen.exe"+"\"" +""+ Filename +"" +Resname; P.standardinput.writeline (str);//Parameters with spaces should be quoted to handle P.standardinput.writeline ( "" // here to exit two times // exit Visual Studio to Cmd.exe P.standardinput.writeline ( " Exit "); // exit cmd.exe P.standardinput.writeline ( "exit" ); p.WaitForExit (); s = s + P.standardoutput.readtoend (); P.close (); // return s;}       
  Public Static stringStartcmd (stringcommand) {            stringOutput =""; Try{Process cmd=NewProcess (); Cmd. Startinfo.filename=command; Cmd. Startinfo.useshellexecute=false; Cmd. Startinfo.redirectstandardinput=true; Cmd. Startinfo.redirectstandardoutput=true; Cmd. Startinfo.createnowindow=true; Cmd. Startinfo.windowstyle=Processwindowstyle.hidden; Cmd.                Start (); Output=cmd.                Standardoutput.readtoend ();                Console.WriteLine (output); Cmd.                WaitForExit (); Cmd.            Close (); }            Catch(Exception e) {Console.WriteLine (e); }            returnoutput; }         Public Static stringStartcmd (stringCommandstringargument) {            stringOutput =""; Process cmd=NewProcess (); Try{cmd. Startinfo.filename=command; Cmd. Startinfo.arguments=argument; Cmd. Startinfo.useshellexecute=false; Cmd. Startinfo.redirectstandardinput=true; Cmd. Startinfo.redirectstandardoutput=true; Cmd. Startinfo.createnowindow=true; Cmd. Startinfo.windowstyle=Processwindowstyle.hidden; Cmd.                Start (); Output=cmd.                Standardoutput.readtoend ();                Console.WriteLine (output); Cmd.                WaitForExit (); Cmd.            Close (); }            Catch(Exception e) {cmd.                Close ();            Console.WriteLine (e); }            returnoutput; }

C # invokes the command line with a space on the parameter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.