如何通过ProcessStartInfo启动sudo进程? [英] How to launch a process with sudo by ProcessStartInfo?
本文介绍了如何通过ProcessStartInfo启动sudo进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在/ etc / sudoers中,我添加了以下行:
myuser ALL = NOPASSWD:/ myprogram
so sudo ./myprogram
适用于myuser。
NET我在代码中使用
string fileName =/ myprogram;
ProcessStartInfo info = new ProcessStartInfo(fileName);
...
我该怎么做sudo fileName?它不工作的时候...
谢谢你,Monique。
解决方案
以下为我工作在类似的情况下,并演示传递多个参数:
var psi = new ProcessStartInfo
{
FileName =/ bin / bash,
UseShellExecute = false,
RedirectStandardOutput = true,
Arguments = string.Format( - c \sudo {0} {1} {2 } \,/ path / to / script,arg1,arg2)
};
使用(var p = Process.Start(psi))
{
if(p!= null)
{
var strOutput = p。 StandardOutput.ReadToEnd();
p.WaitForExit();
}
}
I have a program in Debian which needs root privileges and myuser has to run it, but I have to do the call from a .NET application (C#) running in mono. In /etc/sudoers, I have add the line:
myuser ALL = NOPASSWD: /myprogram
so sudo ./myprogram
works for myuser.
In. NET I use in my code
string fileName = "/myprogram";
ProcessStartInfo info = new ProcessStartInfo (fileName);
...
How can I do the call "sudo fileName"? It doesn't work by the time... thank you, Monique.
解决方案
The following worked for me in a similar situation, and demonstrates passing in multiple arguments:
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();
}
}
这篇关于如何通过ProcessStartInfo启动sudo进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文