我在 Debian 中有一个程序需要 root 权限,我的用户必须运行它,但我必须从以单声道运行的 .NET 应用程序 (C#) 进行调用。 在/etc/sudoers 中,我添加了以下行:
myuser ALL = NOPASSWD: /myprogram
所以 sudo ./myprogram
适用于 myuser。
在。 NET 我在我的代码中使用
string fileName = "/myprogram";
ProcessStartInfo info = new ProcessStartInfo (fileName);
...
如何调用“sudo fileName”?到时候就不行了... 谢谢你,莫尼克。
最佳答案
以下在类似情况下为我工作,并演示了传递多个参数:
var psi = new ProcessStartInfo
{
FileName = "/bin/bash",
UseShellExecute = false,
RedirectStandardOutput = true,
Arguments = string.Format("-c \"sudo {0} {1} {2}\"", "/path/to/script", "arg1", arg2)
};
using (var p = Process.Start(psi))
{
if (p != null)
{
var strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
}
关于c# - 如何通过 ProcessStartInfo 使用 sudo 启动进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22791033/