-1

If I run this command in a command window, it runs fine giving the results I expect. I first go to the FilePath via CD FilePath. Then run the command with a flag and send the output to the DestPath.

C:\Program Files (x86)\ProgramName\FileName.exe -f > C:\Temp\Test.txt

But, it is not running via the below process. I have gone through lots of forums and tried solutions, but none have worked.

public const string FilePath = @"C:\\Program Files (x86)\\ProgramName\\FileName.exe";
public const string DestPath = @"C:\\Temp\\Test.txt";

public static void GetResults()
{
    ProcessStartInfo process = new ProcessStartInfo
        {
            UseShellExecute = true,
            CreateNoWindow = false,
            FileName = @"cmd",
        };

    string Command = @"/c " + FilePath + " -f > " + DestPath;
    process.Arguments = Command;
    process.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(process);
    Thread.Sleep(2000);

Any suggestions on how to correct this issue or what I may be doing incorrectly?

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
Dev
  • 11
  • 3
  • 1
    Your paths can contain whitespaces (in your example code above, FilePath already does). Thus make sure to put surrounding quotes around the paths in the Command string, otherwise the command line arguments you pass to cmd.exe fall apart (due to a non-quoted path argument containing whitespaces). (Side note: Why do you escape the backslashes when you use verbatim string literals `@"....."`?) –  Dec 20 '18 at 18:21
  • 1
    put full path of where cmd.exe is located : FileName = @"cmd", – jdweng Dec 20 '18 at 18:23
  • Hint: To make troubleshooting easier for yourself, first try to run/mimick your call of cmd.exe with exactly the same arguments as in the Command string of your code in a console. (Like, type in the console: `cmd /c `... followed by the content of the Command string) –  Dec 20 '18 at 18:28
  • This is our Internal developed program not available outside. I was searching more and found this page. "https://community.spiceworks.com/topic/1678114-c-change-directory-and-run-command-in-cmd". This is exactly I needed. But when I run this way, it says "C:\Program" is not an internal program. I think it is not able to parse the complete path. I want to go to: cd C:\Program Files (x86)\ProgramPath Then run the command, to get the results into C:\Temp\Test.txt: \retrieve -f > C:\Temp\Test.txt – Dev Dec 21 '18 at 05:47
  • Here's the code which works but does not output to file... ____________ ProcessStartInfo Process = new ProcessStartInfo { UseShellExecute = false, CreateNoWindow = false, FileName = @"C:\Program Files (x86)\ProgramName\Command.exe", }; string RunCommand = @"/c C:\Program Files (x86)\ProgramName\Command.exe -f > C:\Temp\Test.txt"; Process.Arguments = RunCommand; Process.Start(Process); Console.WriteLine(RunCommand); Console.ReadLine(); – Dev Dec 21 '18 at 06:12
  • The Output in the Console I see is: /c C:\Program Files (x86)\ProgramName\Command.exe -f > C:\Temp\Test.txt Results: Line 1 Line 2 Line 3 Line 4 – Dev Dec 21 '18 at 06:13
  • Hey there! I've edited your question to try hopefully make it more readable and get you a better answer. Good luck! – Jeremy Harris Dec 22 '18 at 20:41

1 Answers1

0

So, ultimately, I believe your problem is trying to start the command prompt and getting it to do output redirection. I'm honestly not sure this is supported, and it's not a C# question. If you really really want to do it this way, I recommend writing a small batch file (or maybe a powershell script) which does what you want and takes the parameters you need... probably only the output filename, then start that directly.

That said, let me recommend, please, that you instead use the output redirection built into the process class:

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardoutput?view=netframework-4.7.2

You can then read the output directly into your program and do whatever you want with it... which could be just to write it out to a file... but I strongly suspect that you really want to do something internally to it and that it never actually needs to be written to a file anyway (since you're writing it to a temp folder).

Reginald Blue
  • 930
  • 11
  • 26
  • and all. Thank you for the inputs. I figured out via the below process and it is working all good as I needed. The only issue is, even though I have descripbed this as "CreateNoWindows = True", a console window still flashes. – Dev Dec 21 '18 at 17:57
  • public const string output = "C:\Temp\Test.txt"; Process p = new Process(); p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p = Process.Start(process); p.StandardInput.WriteLine(@"cd " + FilePath); p.StandardInput.WriteLine(@"Retrieve -f > " + output); p.StandardInput.WriteLine(@"EXIT"); string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); – Dev Dec 21 '18 at 17:59
  • Is there any solution with the above fixed code to not flash the console? If I set the "UseShellExecute = True", it gives error for "RedirectStandardOutput" needs UseShellExecute as False. I am kind of stuck with this piece here with no solutions. – Dev Dec 23 '18 at 15:36