Mehmet
Mehmet

Reputation: 95

C# How to run exe in PowerShell

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();

Upvotes: 1

Views: 1411

Answers (2)

Dragos Stoica
Dragos Stoica

Reputation: 1935

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();

Upvotes: 1

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

Upvotes: 1

Related Questions