none
Process & CMD: Not Accepting Programs with Parenthesis RRS feed

  • Question

  • I am trying to execute a program that has parenthesis in the program name, e.g. a program like "append(-k).exe" or "exiftool(-k).exe". So I am setting this up like:

                Process process = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.CreateNoWindow = false;
                startInfo.FileName = "cmd";
                startInfo.Arguments = "/k" + "\"append(-k).exe\"";
                startInfo.WorkingDirectory = "C:\\append\\Release";
                startInfo.UseShellExecute = true;
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();

    When I execute this I get the err: 'append is not recognized as an internal or external command, operable program or batch file. When I go to a regular DOS prompt and execute append(-k).exe I get the same error. However, when I put it in quotation mark, i.e. "append(-k).exe" it works as expected. So I also tried to add the quotation marks to startInfo.Argument as can be seen with the \". However, I still get the same errors as if the quotation marks were not there. Why does this happen?

    Sunday, February 2, 2020 9:45 PM

Answers

  • I used the cmd.exe escape character (^) to escape the first parenthesis as follows-

    Process process = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.FileName = "cmd";
    startInfo.Arguments = "/k append^(-k).exe";
    startInfo.WorkingDirectory = @"F:\RLWA32\Appendk\Debug";
    startInfo.UseShellExecute = true;
    process.StartInfo = startInfo;
    process.Start();
    

    cmd.exe then ran the  append(-k).exe executable successfully

    • Marked as answer by hfaun Monday, February 3, 2020 5:04 AM
    Monday, February 3, 2020 3:19 AM

All replies

  • Hello,

    Seems odd to have () and - characters in the file name, have you tried this without () and - ?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, February 3, 2020 12:16 AM
    Moderator
  • Yes, I have tried without the (-k) and then it works fine. The problem is that the program I am using is from a 3rd party and it behaves differently with the (-k) and without the (-k). I need the behavior of the (-k) version.

    Having said all that, the program behaves just fine in a DOS prompt when it is put in between quotation marks. So why does that not work with the Process task in C#? Using the Process task it doesn't seem to make a difference if the command (actually the property is called Filename) is in quotation marks of not.

    PS: I have tried this with a DOS/Console program I wrote myself and I could duplicate this behavior. If there is a (-k) the Process task fails. 
    • Edited by hfaun Monday, February 3, 2020 1:12 AM
    Monday, February 3, 2020 1:11 AM
  • So here is what I did, created a project named append(-k) and a project named app, both window form apps in the same Visual Studio solution.

    Everything ran as it should, zero issues.

    private void button1_Click(object sender, EventArgs e)
    {
        var fileName = "C:\\Dotnet\\ForumResponses3\\append(-k)\\bin\\Debug\\append(-k).exe";
    
        Process process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            CreateNoWindow = false,
            FileName = fileName,
            Arguments = "/k" + "\"append(-k).exe\"",
            WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
            UseShellExecute = true
        };
    
        process.StartInfo = startInfo;
        process.Start();
        process.WaitForExit();
    
        MessageBox.Show("Done");
    
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, February 3, 2020 2:09 AM
    Moderator
  • I used the cmd.exe escape character (^) to escape the first parenthesis as follows-

    Process process = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.FileName = "cmd";
    startInfo.Arguments = "/k append^(-k).exe";
    startInfo.WorkingDirectory = @"F:\RLWA32\Appendk\Debug";
    startInfo.UseShellExecute = true;
    process.StartInfo = startInfo;
    process.Start();
    

    cmd.exe then ran the  append(-k).exe executable successfully

    • Marked as answer by hfaun Monday, February 3, 2020 5:04 AM
    Monday, February 3, 2020 3:19 AM
  • RLWA32, you are my hero. That works! How did you know to escape this with a ^? I worked hours on this... When I drag 'append(-k).exe' into a CMD window it puts quotation marks around it and that does the trick. For some reason that doesn't seem to work for Process.
    Monday, February 3, 2020 5:04 AM