Home > Back-end >  C# How to run exe in PowerShell
C# How to run exe in PowerShell

Time:04-08

I want to run mc.exe using by PowerShell as I write below.

How can I do that? I tried to add in Filename but it doesn't work.

var mcExe = @"C:\Users\developer\Desktop\Example\mc.exe ";

var proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = mcExe;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Verb = "runas";
proc.StartInfo.Arguments = String.Format("{0}{1}{2}", "./mc alias set myCloud http://localhost:9000", "admin", "123456");
proc.Start();

CodePudding user response:

Did you try set proc.StartInfo.UseShellExecute = true; ? Because this property responsible for using powershell

CodePudding user response:

Starting Powershell directly might work for you, e.g. :

using System.Diagnostics;

ProcessStartInfo startInfo = new ProcessStartInfo
{
    FileName = @"powershell.exe",
    Arguments = @"& 'C:\Users\developer\Desktop\Example\mc.exe' @('./mc alias set myCloud http://localhost:9000', 'admin', '123456')",
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    Verb = "runas",
};
Process process = new Process();
process.StartInfo = startInfo;
process.Start();

string output = process.StandardOutput.ReadToEnd();

string errors = process.StandardError.ReadToEnd();
  • Related