C# Process以指定用户打开程序

//这个是个以管理员用户打开cmd执行一串命令的实例

            string strPWD = "";//你要调用的指定系统用户的密码,为了安全起见,可以事先加密放在INI或者XML文件中,再以解密方式获取。
            SecureString password = new SecureString();   //SecureString,安全字符,必须是char类型
            foreach (char c in strPWD.ToCharArray()) 
            {
                password.AppendChar(c);
            }
            Process P = new Process();
            P.StartInfo.UseShellExecute = false;  //这条属性必须设置为false,否则无法以用户模式运行程序
            P.StartInfo.RedirectStandardError = true;  //以下3条属性MSDN上有详解
            P.StartInfo.RedirectStandardInput = true;
            P.StartInfo.RedirectStandardOutput = true;
            P.StartInfo.CreateNoWindow = false;  //是否显示运行窗口
            P.StartInfo.FileName = "cmd.exe"; //调用的程序名
            P.StartInfo.UserName = "administrator"; //指定用户名
            P.StartInfo.Password = password; //指定明码,必须是安全字符串
            P.StartInfo.Domain = "administrators"; //指定用户名所在域,即所在用户组
            P.StartInfo.Arguments = "/c"+"shutdown -f -s"; //调用程序执行的命令
            P.Start();

转载于:https://www.cnblogs.com/AndyBlue/p/3202152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值