unable to run the command using c#

Viewed 45

I need to run below TXL command using C#. basically i just run a command and store the output in temp.grm. If I run this command in cmd it works fine which means there is no error with the command.

The problem is when I try to run this command using C# program where it just creates a blank temp.grm. Can anyone please help me with this. Thanks!

command: txl -q C:\Users\karth\Desktop\TXL\java.grm C:\Users\karth\Desktop\TXL\Tools\RemoveTxlComments\grm.txl -s 1000 > C:\Users\karth\Desktop\TXL\Temp\temp.grm

...
/* command =        command "txl -q C:\\Users\\karth\\Desktop\\TXL\\java.grm C:\\Users\\karth\\Desktop\\TXL\\Tools\\RemoveTxlComments\\grm.txl -s 1000 > C:\\Users\\karth\\Desktop\\TXL\\Temp\\temp.grm"    string*/

        public bool ExecuteCommand(string command)
        {
            try
            {
                _cmdPrompt.UseShellExecute = true;
                _cmdPrompt.WorkingDirectory = _system32;
                _cmdPrompt.FileName = _cmdPromptPath;
                _cmdPrompt.Verb = "runas";
                _cmdPrompt.Arguments = "/c " + command;
                _cmdPrompt.WindowStyle = ProcessWindowStyle.Hidden;

                Process.Start(_cmdPrompt);
                return true;
            }
            catch (Exception e)
            {
                _logger.LogMessage(e.Message + _newLine);
                return false;
            }
        }
1 Answers

The value of command has spaces in it. The /c argument will only use the next argument as the command to execute, so it will ignore everything after the first space. Assuming there are no quotes in the command to execute, surrounding it with quotes should fix it:

_cmdPrompt.Arguments = "/c \"" + command + "\"";
Related