Quantcast
Channel: yotiky Tech Blog
Viewing all articles
Browse latest Browse all 60

C# - 管理者権限でコマンドプロンプトを実行する

$
0
0

VerbRunAsを設定して、UseShellExecutetrueにする。 Hiddenが設定されている時に、WaitForExitで待機すると処理が終了しない可能性があるので注意。

publicstaticclassCmd
{
    publicstaticvoid CreateSymbolicLink(string src, string dest)
    {
        Execute($"/k mklink /D \"{dest}\"\"{src}\"");
    }

    privatestaticvoid Execute(string args, bool hidden =true)
    {
        var startInfo =new System.Diagnostics.ProcessStartInfo();
        startInfo.FileName ="cmd.exe";
        startInfo.Arguments = args;
        startInfo.Verb ="RunAs";
        startInfo.UseShellExecute =true;
        if (hidden) startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        System.Diagnostics.Process.Start(startInfo);
    }
}

Viewing all articles
Browse latest Browse all 60

Trending Articles