2

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;
            }
        }
  • I) There may be a permissions issue. ii) Please ensure that all paths, etc. are spelled correctly, even a small discrepancy would cause it to fail. – JosephDoggie Jun 17 '19 at 19:16
  • 1
    Thank you!, yes the command is correct, I verified it by taking the value from Debug-Watch and I ran it manually in command prompt. it worked. When running C# code I got a windows permission dialog to grant permission to run the command, which I granted. No luck on getting the output in temp file – Karthik Vishwambar Jun 17 '19 at 19:24

1 Answers1

1

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 + "\"";
Andy
  • 30,088
  • 6
  • 78
  • 89