亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何從 c# 代碼中的 whoami.exe 結果中獲取登錄 ID

我如何從 c# 代碼中的 whoami.exe 結果中獲取登錄 ID

C#
慕沐林林 2021-09-19 16:06:11
如果我打開 cmd 并輸入whoami/logonid我會得到一個登錄 ID 號,經過一番研究,我發現了這一行:var logonId = UserPrincipal.Current.Sid;這段代碼為我提供了一個以 whoami/lgonid 開頭的數字,但它們不同。我不想運行 whoami throw c# 代碼,我只需要獲取結果編號。例如:如果我寫 whoami/user 我得到了用戶名,那么 C# 代碼中的等價物是 WindowsIdentity.GetCurrent().Name;我需要同樣的登錄名
查看完整描述

2 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

ProcessStartInfo.RedirectStandardOutput在實例化 whoami 時使用。您可以解析為 logonid 獲得的文本。


查看完整回答
反對 回復 2021-09-19
?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

您可以使用ProcessStartInfo.RedirectStandardInput和ProcessStartInfo.RedirectStandardoutput屬性來實現這一點。


以下是示例代碼:


public static string GetLoginId()

{

    Process p = new Process();

    p.StartInfo.UseShellExecute = false;

    p.StartInfo.RedirectStandardInput = true;

    p.StartInfo.RedirectStandardOutput = true;

    p.StartInfo.FileName = "cmd.exe";

    p.Start();


    StreamWriter myStreamWriter = p.StandardInput;

    myStreamWriter.WriteLine("whoami /logonid");


    // To avoid deadlocks, always read the output stream first and then wait.

    string output = p.StandardOutput.ReadToEnd();

    p.WaitForExit();

    Console.WriteLine(output);

}


查看完整回答
反對 回復 2021-09-19
  • 2 回答
  • 0 關注
  • 302 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號