c# - 如何通过 ProcessStartInfo 使用 sudo 启动进程?

标签 c# mono debian processstartinfo

我在 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/

相关文章:

c# - swig + 单声道 : C# example errors of not finding the library

asp.net - Mono XSP 忽略 Web.Config 中的 <location> 指令

perl - 使用 apt-get 检查要安装的软件包列表

c# - CSharpRepl emacs 集成?

java - 如何使用 Undertow 启用跨域源

mysql - 对 MySQL 的攻击——以及如何防止它?

c# - 将两个 mp3 音频文件合并为一个

c# - ASP.NET登录控件连接字符串错误: Keyword not supported: 'metadata'

c# - MaxDegreeOfParallelism 决定最优值

c# - 用于分隔函数中参数的正则表达式