2012-12-29 23 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); 
相关问题