gpt4 book ai didi

c# - 在控制台应用程序中编写 cmd 命令

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:09 26 4
gpt4 key购买 nike

我正在尝试编写一个 C# 控制台应用程序,它将通过 cmd 启动 runas.exe,然后以该用户身份运行另一个应用程序。我采纳了下面列出的其中一项建议(并添加了一点),因为它看起来最有希望。

Process cmd = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo("cmd.exe", @"/K C:\Windows\System32\runas.exe /noprofile /user:DOMAIN\USER'c:\windows\system32\notepad.exe\'")
{
RedirectStandardInput = true,
UseShellExecute = false
};
cmd.StartInfo = startinfo;
cmd.Start();
StreamWriter stdInputWriter = cmd.StandardInput;
stdInputWriter.Write("PASSWORD");
cmd.WaitForExit();

当我启动应用程序时,它在命令本身之前要求输入密码,导致 runas.exe

出现错误

enter image description here

我很确定 UseShellExecute = false 是导致错误的原因,但是 StreamWriter 没有它就无法工作,所以我不确定该怎么做。

最佳答案

/c 参数运行命令行并终止,所以你看不到结果(而且它是一个大 C),见她:http://ss64.com/nt/cmd.html

尝试使用“/K”。我用你的命令完成了,我在另一个窗口中看到了结果。

ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/K ping PC -t");
Process.Start(startInfo);

关于c# - 在控制台应用程序中编写 cmd 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40812223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com