通过另一个进程以管理员身份杀死进程
Killing process as admin via another process
我正在尝试通过 C# 中的名称(我已经知道的特定名称)终止某些进程。我找到它们并用 Process.Kill()
杀死它们,但有时在某些进程中我得到 'access denied'。我想这是因为我没有 运行 以管理员的身份管理他们。
我创建了一个执行相同操作的批处理,如果我 运行 以管理员身份将其全部杀死,否则不会。
我可以通过 c# 代码以管理员身份 运行 批处理,即:
var psi = new ProcessStartInfo();
psi.Verb = "runas"; //This suppose to run the command as administrator
//Then run a process with this psi
我的问题是,这真的是解决访问问题的方法吗?有没有更好的办法?如果我以管理员身份 运行 我的 C# 代码,Process.Kill()
是否应该具有与使用批处理文件相同的结果?
你说的是提升权限。
您需要能够找到程序并将杀戮发送到始终 运行 提升的程序。最可靠的方法是将此要求添加到 Programm Manifest。 UAC 会阅读这些内容来帮助您。
第二个最可靠的方法是检查您是否获得了权限。如果没有,让程序尝试(重新)启动自己提升。前一段时间我确实为此写了一些示例代码:
using System;
using System.Diagnostics;
using System.IO;
namespace RunAsAdmin
{
class Program
{
static void Main(string[] args)
{
/*Note: Running a batch file (.bat) or similar script file as admin
Requires starting the interpreter as admin and handing it the file as Parameter
See documentation of Interpreting Programm for details */
//Just getting the Absolute Path for Notepad
string windir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
string FullPath = Path.Combine(windir, @"system32\notepad.exe");
//The real work part
//This is the programm to run
ProcessStartInfo startInfo = new ProcessStartInfo(FullPath);
//This tells it should run Elevated
startInfo.Verb = "runas";
//And that gives the order
//From here on it should be 100% identical to the Run Dialog (Windows+R), except for the part with the Elevation
System.Diagnostics.Process.Start(startInfo);
}
}
}
请注意,无论您如何尝试提升,在非常罕见的 OS 设置中,提升都会失败。
我正在尝试通过 C# 中的名称(我已经知道的特定名称)终止某些进程。我找到它们并用 Process.Kill()
杀死它们,但有时在某些进程中我得到 'access denied'。我想这是因为我没有 运行 以管理员的身份管理他们。
我创建了一个执行相同操作的批处理,如果我 运行 以管理员身份将其全部杀死,否则不会。
我可以通过 c# 代码以管理员身份 运行 批处理,即:
var psi = new ProcessStartInfo();
psi.Verb = "runas"; //This suppose to run the command as administrator
//Then run a process with this psi
我的问题是,这真的是解决访问问题的方法吗?有没有更好的办法?如果我以管理员身份 运行 我的 C# 代码,Process.Kill()
是否应该具有与使用批处理文件相同的结果?
你说的是提升权限。
您需要能够找到程序并将杀戮发送到始终 运行 提升的程序。最可靠的方法是将此要求添加到 Programm Manifest。 UAC 会阅读这些内容来帮助您。
第二个最可靠的方法是检查您是否获得了权限。如果没有,让程序尝试(重新)启动自己提升。前一段时间我确实为此写了一些示例代码:
using System;
using System.Diagnostics;
using System.IO;
namespace RunAsAdmin
{
class Program
{
static void Main(string[] args)
{
/*Note: Running a batch file (.bat) or similar script file as admin
Requires starting the interpreter as admin and handing it the file as Parameter
See documentation of Interpreting Programm for details */
//Just getting the Absolute Path for Notepad
string windir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
string FullPath = Path.Combine(windir, @"system32\notepad.exe");
//The real work part
//This is the programm to run
ProcessStartInfo startInfo = new ProcessStartInfo(FullPath);
//This tells it should run Elevated
startInfo.Verb = "runas";
//And that gives the order
//From here on it should be 100% identical to the Run Dialog (Windows+R), except for the part with the Elevation
System.Diagnostics.Process.Start(startInfo);
}
}
}
请注意,无论您如何尝试提升,在非常罕见的 OS 设置中,提升都会失败。