Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以管理员的身份运行CMD,然后使用C中的参数运行exe#_C#_Cmd_Exe - Fatal编程技术网

C# 如何以管理员的身份运行CMD,然后使用C中的参数运行exe#

C# 如何以管理员的身份运行CMD,然后使用C中的参数运行exe#,c#,cmd,exe,C#,Cmd,Exe,您好,我正在尝试编写一个C#程序,以便CMD以管理员身份打开,然后使用参数运行一个可执行文件。下面的代码可以打开CMD(在管理模式下),但cPath是我指向可执行文件的路径。问题是我无法将参数传递给可执行文件。有特殊的语法吗 Process cmd = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", "c/c " + cPath with a

您好,我正在尝试编写一个C#程序,以便CMD以管理员身份打开,然后使用参数运行一个可执行文件。下面的代码可以打开CMD(在管理模式下),但cPath是我指向可执行文件的路径。问题是我无法将参数传递给可执行文件。有特殊的语法吗

             Process cmd = new Process();
             ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", "c/c " + cPath with argument);
             startInfo.Verb = "runas";
             cmd.StartInfo.UseShellExecute = false;
             cmd.StartInfo = startInfo;
             cmd.Start();

如果是
cmd.exe
,则需要通过
/c
参数传递。 因此,在您的情况下,它将是:

var args = "some_args_to_exe";
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", $"/c \"{cPath}\" \"{args}\"");
startInfo.Verb = "runas";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo = startInfo;
cmd.Start();

您可以这样做,在没有窗口的情况下启动命令行并捕获文本输出

static object ResultLock = new object();

/// <summary>
/// 
/// </summary>
/// <param name="executable"></param>
/// <param name="args"></param>
/// <param name="exitCode"></param>
/// <returns></returns>
private static string ExecuteCommand(string executable, string args, out int exitCode)
{
    exitCode = -1;

    // create the process
    var proc = new Process
    {

        EnableRaisingEvents = true,
        StartInfo =
        {
            FileName = executable,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            Arguments = args,
            CreateNoWindow = true,
            WindowStyle = ProcessWindowStyle.Hidden,
            Verb = "runas"
        }
    };

    // try to start the process
    try
    {
        proc.Start();
    }
    catch (Exception ex)
    {
        return
            $"Error launching command line {executable}\n\n{ex.Message}";
    }

    var result = false;
    var messageString = new StringBuilder();
    var timeBefore = DateTime.Now;

    // Wait for process
    while (false == result && (DateTime.Now - timeBefore < new TimeSpan(0, 0, 60)))
    {
        result = proc.WaitForExit(100);
        messageString.Append(proc.StandardOutput.ReadToEnd());
    }

    if (result)
    {

        var message = messageString.ToString();

        lock (ResultLock)
        {
            // save the exitcode
            exitCode = proc.ExitCode;
        }

        return message;
    }
    try
    {
        proc.Close();
    }
    catch
    {
        // ignored
    }
    return $"Error {executable} timed out";
}
static object ResultLock=new object();
/// 
/// 
/// 
/// 
/// 
/// 
/// 
私有静态字符串ExecuteCommand(字符串可执行文件、字符串参数、out int exitCode)
{
exitCode=-1;
//创建流程
var proc=新流程
{
EnableRaisingEvents=true,
StartInfo=
{
FileName=可执行文件,
重定向标准输出=真,
UseShellExecute=false,
参数=args,
CreateNoWindow=true,
WindowsStyle=ProcessWindowsStyle.Hidden,
动词=“符文”
}
};
//试着开始这个过程
尝试
{
proc.Start();
}
捕获(例外情况除外)
{
返回
$“启动命令行{executable}时出错\n\n{ex.Message}”;
}
var结果=假;
var messageString=new StringBuilder();
var timeBefore=DateTime.Now;
//等待进程
while(false==result&(DateTime.Now-timeBefore
ProcessStartInfo.Arguments