2012-12-29 25 views
4

即使路徑使用雙引號,Windows 8上的路徑中存在空格時,我在C#中啓動進程時出現問題,C#在Windows 8上使用路徑中的空格執行外部進程

下面的代碼一直工作正常多年來對我們的XP和Windows 7的機器,但我們最近更換了一些發展盒上到Windows 8,現在我們得到以下錯誤:

'D:\Workspace\Visual' is not recognized as an internal or external command, operable program or batch file.

代碼:

string command = "\"D:\\Workspace\\Visual Studio 2010\\Dev\\Tools\\Editors\\AssetManager\\bin\\Tools\\TextureAtlasBuilder.exe\""; 
string arguments = "\"D:\\Local\\Temp\\xu2twc4d.cg1\" \"environment-textures\""; 

ProcessStartInfo startInfo = new ProcessStartInfo 
{ 
    CreateNoWindow = true, 
    UseShellExecute = false, 
    WindowStyle = ProcessWindowStyle.Hidden, 
    RedirectStandardError = true, 
    RedirectStandardOutput = true, 
    FileName = string.Format(CultureInfo.InvariantCulture, @"{0}\cmd.exe", Environment.SystemDirectory), 
    Arguments = string.Format(CultureInfo.InvariantCulture, "/C {0} {1}", command, arguments) 
}; 

Process process = new Process 
{ 
    StartInfo = startInfo 
}; 

process.OutputDataReceived += new DataReceivedEventHandler(StandardOutputHandler); 
process.ErrorDataReceived += new DataReceivedEventHandler(StandardErrorHandler); 

process.Start(); 

我試着和文字字符串沒有雙引號,逐字字符串與不加雙引號,我總是得到同樣的錯誤!

我在做什麼錯?

感謝

+0

你真的必須首先啓動cmd嗎? – rene

+0

你可以嘗試刪除「從你的參數字符串 – rene

+0

我同意在代碼示例中,我可能不需要啓動cmd,但這是用於啓動不一定exe文件的不同工具的通用代碼(例如作爲Java或腳本程序)我已經測試了一個Java程序,我得到了同樣的錯誤:-( – Atogus

回答

1

刪除「從參數字符串。

這是棘手的,因爲與/ C cmd的行爲是非常嚴格的使用」如可以發現出從CMD/?.如果全部失敗:將命令行和參數寫入臨時cmd文件並啓動該文件...

If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters:

1. If all of the following conditions are met, then quote characters 
    on the command line are preserved: 

    - no /S switch 
    - exactly two quote characters 
    - no special characters between the two quote characters, 
     where special is one of: &<>()@^| 
    - there are one or more whitespace characters between the 
     two quote characters 
    - the string between the two quote characters is the name 
     of an executable file. 

2. Otherwise, old behavior is to see if the first character is 
    a quote character and if so, strip the leading character and 
    remove the last quote character on the command line, preserving 
    any text after the last quote character. 
0

我使用下面的路徑,它成功地接受。

string command1 = @"C:\test\ab ab\1.txt"; 
Process.Start(command1); 
相關問題