Process.Start() not starting the .exe file (works when run manually)
Asked Answered
U

3

26

I have an .exe file that needs to be run after I create a file. The file is successfully created and I am using the following code to run the .exe file after that:

ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = pathToMyExe;
processInfo.ErrorDialog = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;                        
Process proc = Process.Start(processInfo);

I also tried with a simple Process.Start(pathToMyExe); but the .exe file is not run. When I try pathToMyExe manually on my Windows Explorer the program is correctly run. But not via the program. What I see is the cursor turning to waiting for a few seconds and then back to normal. So there are no Exceptions thrown either. What is blocking the file?

Usually answered 27/7, 2015 at 9:2 Comment(3)
My psychic powers are telling me you need to set the WorkingDirectory.Propose
You sure it's not running? You redirected standard output, but you didn't post any code related to handling the redirected output. You therefore won't see any output unless you handle it specially. I'm assuming this is a Console App btw.Sievert
@Propose you are right. You can post it as the answer.Usually
P
43

You are not setting the working directory path, and unlike when starting the application through Explorer, it isn't set automatically to the location of the executable.

Just do something like this:

processInfo.WorkingDirectory = Path.GetDirectoryName(pathToMyExe);

(assuming the input files, DLLs etc. are in that directory)

Propose answered 27/7, 2015 at 10:33 Comment(1)
processInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;Inefficacy
C
1

Due to different working directory, you have to set your working directory properly to the path that you want your process to start.

A sample demonstration of this can be:

Process process = new Process()
{
    StartInfo = new ProcessStartInfo(path, "{Arguments If Needed}")
    {
        WindowStyle = ProcessWindowStyle.Normal,
        WorkingDirectory = Path.GetDirectoryName(path)
    }
};

process.Start();
Cymene answered 19/10, 2016 at 11:32 Comment(0)
T
1
    private void Print(string pdfFileName)
    {
        string processFilename = Microsoft.Win32.Registry.LocalMachine
    .OpenSubKey("Software")
    .OpenSubKey("Microsoft")
    .OpenSubKey("Windows")
    .OpenSubKey("CurrentVersion")
    .OpenSubKey("App Paths")
    .OpenSubKey("AcroRd32.exe")
    .GetValue(string.Empty).ToString();

        ProcessStartInfo info = new ProcessStartInfo();
        info.Verb = "print";
        info.FileName = processFilename;
        info.Arguments = string.Format("/p /h {0}", pdfFileName);
        info.CreateNoWindow = true;
        info.WindowStyle = ProcessWindowStyle.Hidden;
        ////(It won't be hidden anyway... thanks Adobe!)
        info.UseShellExecute = false;

        Process p = Process.Start(info);
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        int counter = 0;
        while (!p.HasExited)
        {
            System.Threading.Thread.Sleep(1000);
            counter += 1;

            if (counter == 5)
            {
                break;
            }
        }

        if (!p.HasExited)
        {
            p.CloseMainWindow();
            p.Kill();
        }
    }
Twoway answered 29/6, 2017 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.