C # Running an external process with spaces in the path in Windows 8

I had a problem starting a process in C # when there is a space in the path in Windows 8, even if the path is in double quotes!

The following code has been running for many years on our XP and Windows 7 computers, but we recently switched some development boxes to Windows 8, and now we get the following error:

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

code:

 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(); 

I tried literal strings with double quotes and without them, shorthand strings with double quotes and without them, and I always get the same error!

What am I doing wrong?!

thanks

+4
source share
2 answers

Remove line from argument line.

This is complicated because the behavior of cmd with / C is very strict in using "how can one learn from cmd / ?. If all else fails: write your command line and arguments to the temporary CMD file and run this ...

If / C or / K is specified, the rest of the command line after the switch is treated 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. 
+1
source

I use the following path and it successfully accepts.

 string command1 = @"C:\test\ab ab\1.txt"; Process.Start(command1); 
0
source

All Articles