C# 調用命令列,參數有空格

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   sp   檔案   

 在程式中調用cmd命令開啟一個檔案,而檔案路徑帶有空格,如果直接把路徑傳給cmd,那麼cmd就會把路徑空格前面的部分當做是一個參數,空格後當做另一個參數,命令列執行把後邊截掉了,導致程式出錯,會彈出了C:\Program 不是內部或外部命令,也不是可啟動並執行程式或批次檔的錯誤提示。解決方案是把傳入的參數前後添加雙引號,如下:

 

  private static void ResxToRes(ArrayList ResxPath)        {            //ResxFile 是一個檔案夾,用來存放 需要轉換的.resx 檔案            string s = "";            Process p = new Process();            p.StartInfo.FileName = "cmd.exe";            p.StartInfo.UseShellExecute = false;            p.StartInfo.RedirectStandardInput = true;            p.StartInfo.RedirectStandardOutput = true;            p.StartInfo.CreateNoWindow = false;            p.Start();            // + ‘"‘ + ‘"‘ + 可以用 \"\"   替換           // 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();               //根據需要寫相應演算法組建檔案名;                ////resgen E:\Filename1.resx e:\ResName1.resources               // p.StandardInput.WriteLine("ResGen" + Filename + " " + System.IO.Path.GetFileNameWithoutExtension(Filename) + @".resx");\string                 string ResName =Path.GetFullPath(Filename)+ ".resx";                 string str = "\""+@"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ResGen.exe"+"\"" + "  " + Filename + " " + ResName;                 p.StandardInput.WriteLine(str);//參數帶有空格 應加上引號處理                p.StandardInput.WriteLine(" ");            }            //此處要exit兩次            //退出visual studio 到 cmd.exe            p.StandardInput.WriteLine("exit");            //退出cmd.exe            p.StandardInput.WriteLine("exit");            p.WaitForExit();            s = s + p.StandardOutput.ReadToEnd();            p.Close();           // return s;        }
 public static string startcmd(string command)        {            string output = "";            try            {                Process cmd = new Process();                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);            }            return output;        }        public static string startcmd(string command, string argument)        {            string output = "";             Process cmd = new Process();            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);            }            return output;        }

 

C# 調用命令列,參數有空格

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.