1

Ok, so as the title implies, I'm having some trouble with this.... When I use the following code it will run but I can't even use > output.txt to get some status of how it ran....

            ProcessStartInfo x = new ProcessStartInfo();
            x.FileName = "somefile.exe";
            x.Arguments = arg1 + " " + arg2 + " " + arg3 + " " + arg4;
            x.WorkingDirectory = workDir;
            x.WindowStyle = ProcessWindowStyle.Hidden;
            Process mde = Process.Start(x);
            mde.WaitForExit();

Now, what's confusing me is that the moment I add in the code for capturing the input, I get thrown an exception stating that the exe file I'm trying to run does not exist. So when I use....

            ProcessStartInfo x = new ProcessStartInfo();
            x.FileName = "somefile.exe";
            x.Arguments = arg1 + " " + arg2 + " " + arg3 + " " + arg4;
            x.WorkingDirectory = workDir;
            x.WindowStyle = ProcessWindowStyle.Hidden;
            modelf.UseShellExecute = false;
            modelf.RedirectStandardOutput = true;
            Process mde = Process.Start(x);
            mde.WaitForExit();

What exactly am I doing wrong here. It's like the working directory property can't be set when the useshellexecute property is used, but from what I read that's not the case. So what's going on? Why can it find the file and execute it properly in the first example and not in the second?

2 Answers 2

2

MSDN Quote from http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx

When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used by the process that is started and only has meaning within the context of the new process.

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

2 Comments

Will I need to add full paths to arguments too?
nvm, found out I only needed to add the path to the filename property. Works great now, thanks.
0

In case anyone else wants to know the way it worked....

        Process x = new Process
        {
            StartInfo =
            {
                FileName = fullPathToExe,
                Arguments = arg1 + " " + arg2 + " " + arg3 + " " + arg4,
                WorkingDirectory = outDir,
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false,
                RedirectStandardOutput = true
            }
        };

        x.Start();
        string output = x.StandardOutput.ReadToEnd();
        x.WaitForExit();

It still blinks up a window, but I figure createnowindow= true will fix that. I figured I'd post the code in case anyone else needed it.

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.