3

I need to do 2 things: run a batch file (works fine), and run a command (does not work). The method for the command throws the exception 'file not found'. If I open a cmd window, and type the command, it works perfectly.

  private static void Rescan()
    {
        //System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("DEVCON ReScan");
        //psi.RedirectStandardOutput = true;
        //psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        //psi.UseShellExecute = false;
        //System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = "DEVCON ReScan";
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.UseShellExecute = false;
        proc.Start();
        proc.WaitForExit();
        System.IO.StreamReader myOutput = proc.StandardOutput;
        proc.WaitForExit(4000);
        if (proc.HasExited)
        {
            string output = myOutput.ReadToEnd();
            FileIO.WriteLog(_writePath, output);
        }

    }

The commented code also throws the same exception.

3 Answers 3

9

DEVCON ReScan is really the name of the executable? I guess the executable is DEVCON, while ReScan is a parameter. This means you have to set StartInfo.FileName to "DEVCON" and StartInfo.Arguments to "ReScan".

Sign up to request clarification or add additional context in comments.

3 Comments

For some funny reason, that's a common mistake with both System.Diagnostics.Process and the native ShellExecute(Ex), even though it's clearly a "FileName" parameter :)
I would set Filename to the filename (devcon.exe) and use proc.StartInfo.Arguments to pass in the ReScan argument. Just to keep it obvious what is what.
I do the same thing on a regular basis when using Process (which is not that often)...
0

Is the DEVCON application actually in the working directory ? Otherwise it won't work unless you specify the full path to it.

Furthermore you have to specify the extension, so I suppose you'd go for "Devcon.exe", and specify the parameters not in the filename but in the parameters :)

1 Comment

Devcon is in a PATH Environment directory. It is working: proc.StartInfo.FileName = "DEVCON"; proc.StartInfo.Arguments = "ReScan";
0

Try this:

        ProcessStartInfo psi = new ProcessStartInfo();            
        psi.FileName = Environment.GetEnvironmentVariable("comspec");
        psi.CreateNoWindow = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.UseShellExecute = false;

        Process p = Process.Start(psi);

        ConsoleColor fc = Console.ForegroundColor;

        StreamWriter sw = p.StandardInput;
        StreamReader sr = p.StandardOutput;

        char[] buffer = new char[1024];
        int l = 0;

        sw.Write("DEVCON ReScan");
        sw.Write(sw.NewLine);

        Console.Write(">> ");

        l = sr.Read(buffer, 0, buffer.Length);

        for (int n = 0; n < l; n++)
            Console.Write(buffer[n] + " ");

        p.Close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.