Warm tip: This article is reproduced from serverfault.com, please click

How to run an exe file using C# in console application

发布于 2020-11-30 09:12:00

I am trying to run an exe file in my console application which is located on a network drive. So what needs to happen is the app needs to map the network drive with a drive letter by using this code:

 private static void MapDrive()
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "net.exe";
        startInfo.Arguments = @"use w: \\<server>\CompanyData\W10 /user:Administrator Password";
        process.StartInfo = startInfo;
        process.Start();
    }

This works great and the drive letter is mapped. Now the problem I am facing is to run the exe file with in this mapped drive. I have tried the below but does not seem to work:

 private static void RunSetup()
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = @"w:\setup.exe";
        process.StartInfo = startInfo;
        process.Start();;
    }

Nothing seems to happen in regards to launching the exe file.
I need to know what I am doing wrong here?

Thanks

Questioner
Keith
Viewed
0
dontbyteme 2020-11-30 17:35:58

Either include the "/c" argument

startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/c w:\setup.exe";

or set FileName directly to the setup.exe

startInfo.FileName = "w:\setup.exe";

as mentioned in the comments