欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#通過(guò)進(jìn)程調(diào)用cmd判斷登錄用戶(hù)權(quán)限代碼分享

 更新時(shí)間:2013年12月27日 16:13:29   作者:  
最近自己開(kāi)發(fā)軟件需要讀取本地配置文件,因?yàn)榈卿浻脩?hù)的權(quán)限不夠會(huì)導(dǎo)致無(wú)法讀取文件進(jìn)而導(dǎo)致程序崩潰,查了一些解決方法,代碼分享如下

復(fù)制代碼 代碼如下:

/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
  if (RunCmd("net localgroup administrators").IndexOf(System.Environment.UserName) >= 0)
    {

    //順利執(zhí)行。
    }
    else
    {
        //報(bào)錯(cuò)提示系統(tǒng)不是管理員用戶(hù)登錄,容易導(dǎo)致程序崩潰。請(qǐng)退出以管理員權(quán)限登錄。

               
        //并退出程序。
     }
}
/// <summary>
/// 調(diào)用cmd.exe程序執(zhí)行命令。
/// </summary>
/// <param name="command">要執(zhí)行的命令</param>
/// <returns></returns>
static string RunCmd(string command)
{
    //實(shí)例一個(gè)Process類(lèi),啟動(dòng)一個(gè)獨(dú)立進(jìn)程
    Process p = new Process();

    //Process類(lèi)有一個(gè)StartInfo屬性,這個(gè)是ProcessStartInfo類(lèi),包括了一些屬性和方法,下面我們用到了他的幾個(gè)屬性:

    p.StartInfo.FileName = "cmd.exe";           //設(shè)定程序名
    p.StartInfo.Arguments = "/c " + command;    //設(shè)定程式執(zhí)行參數(shù)
    p.StartInfo.UseShellExecute = false;        //關(guān)閉Shell的使用
    p.StartInfo.RedirectStandardInput = true;   //重定向標(biāo)準(zhǔn)輸入
    p.StartInfo.RedirectStandardOutput = true;  //重定向標(biāo)準(zhǔn)輸出
    p.StartInfo.RedirectStandardError = true;   //重定向錯(cuò)誤輸出
    p.StartInfo.CreateNoWindow = true;          //設(shè)置不顯示窗口

    p.Start();   //啟動(dòng)
    p.StandardInput.WriteLine("exit");        //不過(guò)要記得加上Exit要不然下一行程式執(zhí)行的時(shí)候會(huì)當(dāng)機(jī)

    return p.StandardOutput.ReadToEnd();        //從輸出流取得命令執(zhí)行結(jié)果
}

相關(guān)文章

最新評(píng)論