0

I'm currently in the process of creating a console application that acts as a Video Management Hub. I'm having issues with passing arguments into command line through process. Every time it returns the output from stdout and stderror using appropriate threads for each it's acting as though the Standard.Error.ReadToEnd() and Standard.Out.ReadToEnd() aren't seeing the full arguments after it's waited for the process to exit. Exception returns "is not recognized as an internal or external command, operable program or batch file." Code snippets below show Open method process.

private void Thread_ReadStandardError()
{
    if (activeProcess != null)
    {
        stdErr = activeProcess.StandardError.ReadToEnd();
    }
}

private void Thread_ReadStandardOut()
{
    if (activeProcess != null)
    {
        stdOut = activeProcess.StandardOutput.ReadToEnd();
    }
}

private string Open(string cmd)
{
    string args = "/C [command]";
    string temp_path = args.Replace("[command]",cmd);

    this.pStartInfo.FileName = "cmd.exe";
    this.pStartInfo.Arguments = "\"" + temp_path + "\"";
    this.activeProcess.StartInfo = pStartInfo;
    this.pStartInfo.CreateNoWindow = true;
    this.pStartInfo.UseShellExecute = false;
    this.pStartInfo.RedirectStandardOutput = true;
    this.pStartInfo.RedirectStandardError = true;
    activeProcess = Process.Start(pStartInfo);

    Thread thread_ReadStandardError = new Thread(new ThreadStart(Thread_ReadStandardError));

    Thread thread_ReadStandardOut = new Thread(new ThreadStart(Thread_ReadStandardOut));

    if (pStartInfo.RedirectStandardError)
    {
        thread_ReadStandardError.Start();
    }
    if (pStartInfo.RedirectStandardOutput)
    {
        thread_ReadStandardOut.Start();
    }
    activeProcess.WaitForExit();

    thread_ReadStandardError.Join();
    thread_ReadStandardOut.Join();

    string output = stdOut + stdErr;

    return output;
}

Ultimately I am trying to use a modified version of ExifToolWrapper to run command line arguments to read in video Metadata. I got appropriate arguments/paths prior to my 'Open' method and handle white space before passing in arguments. Process is relatively new to me and prior I was trying to use EnvironmentalVariables to pass in arguments and I get the same output from stdOut+stdErr of

"C:Users###....is not recognized as an internal or external command..."

Is it possibly the way in which my process in setup?

TreyLD
  • 1
  • 1
  • The problem is not in your code but in whatever you start in [Command] . The error you see is from the cmd.exe that tells you it can't find the [command] you want cmd.exe to execute. Not much we can do about that. Try a full path to whatever [command] is to overcome any path issues. – rene Nov 05 '22 at 08:23
  • Thank you @rene for the quick response. In the [command] being sent to **Open** it looks something like this **C:\Users\Trey Dixonxxxxxxxxxx\Desktop\School\Fall 2022-2023\CIT 458\Content Management Hub - OFFICIAL\ContentManagementHubGUI\ContentManagementHubGUI\bin\Debug"/exiftool.exe-ver** I'm getting the assembly path for the running application from a 'GetAppPath()' method. This then Concats the exiftool.exe to this Assembly Path which then is passed to the 'Open' method. Does full path need escaped? – TreyLD Nov 05 '22 at 16:28
  • You probably end-up double escaping quotes stuff, specially because you wrap the `/C [command]` in double quotes as well. Maybe helpful: https://stackoverflow.com/questions/41277533/c-sharp-use-process-start-with-parameters-and-spaces-in-path/41369918#41369918. Do you really need cmd.exe to run? Can't ProcessInfo start exiftool.exe directly and capture its output? Also: ProcessStartInfo has a [WorkingDirectory](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.workingdirectory?view=net-6.0) that might make it easier. – rene Nov 05 '22 at 16:49
  • Also: you don't need to deal with two threads to capture output, just subscribe to the events and call the BeginOutputReadLine and BeginErrorReadLine methods https://stackoverflow.com/a/15032982 – rene Nov 05 '22 at 16:52
  • After some further changes to my code I have the command line arguments returning output! Opting for **subscribing to events**, utilizing `ProcessStartInfo` and `WorkingDirectory` and using **verbatim string literals**. These several suggestions tremendously helped me out! Thank you so much @rene – TreyLD Nov 06 '22 at 07:18

0 Answers0