4

I have a problem starting a process in C# when there's a space in the path on Windows 8, even when the path is in double quotes!

The following code has been working fine for years on our XP and Windows 7 machines, but we recently switched some development boxes over to Windows 8 and we now 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've tried literal strings with and without double quotes, verbatim strings with and without double quotes, and I always get the same error!

What am I doing wrong?!

Thanks

5
  • do you really have to start cmd first?
    – rene
    Dec 29, 2012 at 14:23
  • can you try to remove the " from your arguments string
    – rene
    Dec 29, 2012 at 14:26
  • I agree that in the code example, I probably don't need to start cmd, but this is generic code used to start different tools which aren't necessarily exe files (such as java or script programs). I've tested with a java program and I get the same error :-(
    – Atogus
    Dec 29, 2012 at 14:30
  • Since I can't yet answer my own question I'll do it here for now: As rene suggested, I've removed the double quotes from the argument list and it works as it should. As for why I only get the error on Windows 8, I have no idea... but at least it works now! Thanks!
    – Atogus
    Dec 29, 2012 at 14:38
  • I added my comments as answer and added my reasoning why I sugested it.
    – rene
    Dec 29, 2012 at 14:48

2 Answers 2

1

Remove the " from your arguments string.

It is tricky because the cmd behavior with /C is very strict on the use of " as can be found out out from cmd /?. If all fails: write your commandline and arguments to a temporary cmd file and start that one...

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

I am using following path, and it is successfully accepting.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.