c#通过进程调用cmd判断登录用户权限代码分享
时间:2021-07-24 00:53:26
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
if (RunCmd("net localgroup administrators").IndexOf(System.Environment.UserName) >= 0)
{
//顺利执行。
}
else
{
//报错提示系统不是管理员用户登录,容易导致程序崩溃。请退出以管理员权限登录。
//并退出程序。
}
}
/// <summary>
/// 调用cmd.exe程序执行命令。
/// </summary>
/// <param name="command">要执行的命令</param>
/// <returns></returns>
static string RunCmd(string command)
{
//实例一个Process类,启动一个独立进程
Process p = new Process();
//Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,下面我们用到了他的几个属性:
p.StartInfo.FileName = "cmd.exe"; //设定程序名
p.StartInfo.Arguments = "/c " + command; //设定程式执行参数
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; //设置不显示窗口
p.Start(); //启动
p.StandardInput.WriteLine("exit"); //不过要记得加上Exit要不然下一行程式执行的时候会当机
return p.StandardOutput.ReadToEnd(); //从输出流取得命令执行结果
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WPF实现控件拖动的示例代码
Java RPC框架熔断降级机制原理解析
![](https://img.aspxhome.com/file/2023/4/76544_0s.png)
如何在Redis中实现分页排序查询过程解析
![](https://img.aspxhome.com/file/2023/0/92660_0s.jpg)
Java创建型设计模式之抽象工厂模式(Abstract Factory)
![](https://img.aspxhome.com/file/2023/4/101764_0s.png)
spring cloud config 配置中心快速实现过程解析
DecimalFormat多种用法详解
详解springboot之jackson的两种配置方式
java基础之包装类的介绍及使用
![](https://img.aspxhome.com/file/2023/5/61255_0s.png)
将应用程序进行Spring6迁移的最佳使用方式
![](https://img.aspxhome.com/file/2023/2/84212_0s.png)
一次排查@CacheEvict注解失效的经历及解决
![](https://img.aspxhome.com/file/2023/0/59200_0s.png)
使用SpringMVC的@Validated注解验证的实现
C#多线程系列之线程的创建和生命周期
![](https://img.aspxhome.com/file/2023/2/77262_0s.jpg)
Java Bean与xml互相转换的方法分析
什么是递归?用Java写一个简单的递归程序
![](https://img.aspxhome.com/file/2023/2/60772_0s.jpg)
Java逃逸分析详解及代码示例
C# form-data上传图片流到远程服务器的详细代码
![](https://img.aspxhome.com/file/2023/9/70409_0s.png)
java实现饮料自助售货机
C#中判断本地系统的网络连接状态的方法
![](https://img.aspxhome.com/file/2023/7/84457_0s.jpg)
解决genymotion模拟器无法联网的正确方法100%成功
![](https://img.aspxhome.com/file/2023/7/89517_0s.png)
Java实现简单的扫雷小程序
![](https://img.aspxhome.com/file/2023/6/63806_0s.jpg)