C# 如何在C语言中将隐藏的输入传递给另一个程序#
我在使用C#以编程方式将输入传递给系统C# 如何在C语言中将隐藏的输入传递给另一个程序#,c#,process,io,C#,Process,Io,我在使用C#以编程方式将输入传递给系统net用户程序时遇到问题。我正在尝试激活并设置某个用户帐户的密码。根据我的调查,这一过程似乎在任何事情通过之前就结束了。我不知道为什么后台net用户程序在退出前不等待输入 以下是我以编程方式运行的命令,以完成此任务: net user username /active:yes & net user username * 第二个命令的输出如下: Type a password for the user: Retype the password to
net用户
程序时遇到问题。我正在尝试激活并设置某个用户帐户的密码。根据我的调查,这一过程似乎在任何事情通过之前就结束了。我不知道为什么后台net用户程序在退出前不等待输入
以下是我以编程方式运行的命令,以完成此任务:
net user username /active:yes & net user username *
第二个命令的输出如下:
Type a password for the user:
Retype the password to confirm:
The command completed successfully
如果您手动运行上述命令,它将询问您密码并在屏幕上隐藏您正在键入的内容。但是,当以编程方式运行时,程序似乎不会停止
为了调用程序,我有一个函数,它启动程序并将进程返回给另一个函数,该函数向进程发送输入。以下是第一个函数:
static Process RunCommandGetProcess(string command)
{
Process process = new Process();
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.FileName = "CMD.exe";
psInfo.Arguments = "/C " + command + "& PAUSE";
// Allow for Input redirection
psInfo.UseShellExecute = false;
psInfo.RedirectStandardInput = true;
// Window style
psInfo.WindowStyle = ProcessWindowStyle.Normal;
// Start the mothertrucker!
process.StartInfo = psInfo;
process.Start();
return process;
}
static int ActivateUserWithPassword(string password)
{
// Start net user with that other function
Process process = RunCommandGetProcess("net user username /active:yes & net user username *");
StreamWriter streamWriter = process.StandardInput;
streamWriter.WriteLine(password); // First Prompt
streamWriter.WriteLine(password); // Second Prompt
process.WaitForExit();
return process.ExitCode;
}
以及调用函数:
static Process RunCommandGetProcess(string command)
{
Process process = new Process();
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.FileName = "CMD.exe";
psInfo.Arguments = "/C " + command + "& PAUSE";
// Allow for Input redirection
psInfo.UseShellExecute = false;
psInfo.RedirectStandardInput = true;
// Window style
psInfo.WindowStyle = ProcessWindowStyle.Normal;
// Start the mothertrucker!
process.StartInfo = psInfo;
process.Start();
return process;
}
static int ActivateUserWithPassword(string password)
{
// Start net user with that other function
Process process = RunCommandGetProcess("net user username /active:yes & net user username *");
StreamWriter streamWriter = process.StandardInput;
streamWriter.WriteLine(password); // First Prompt
streamWriter.WriteLine(password); // Second Prompt
process.WaitForExit();
return process.ExitCode;
}
但是,当我运行调试器时,命令在两个streamWriter.WriteLine(密码)之前成功完成代码>行甚至满足!我试过谷歌搜索,但没有用
你们是我唯一的希望。好吧!我一看到你的问题,就非常有动力去解决它!。经过两个小时的不间断调试,我有了一个解决方案
问题1:您的应用程序没有管理员权限,这就是为什么启动后命令立即退出。以管理员权限启动.exe
第2期:由于密码在输入中被屏蔽,即使我做过一次streamWriter.WriteLine(密码)
输出也是“命令成功执行”。无法知道密码是否已实际传递,或者它是否以空字符串作为密码
解决方案
您可以将net user
命令与密码参数一起使用,如下所示
net user\u name密码
。无需提示用户输入密码,即不要在用户名后使用“*”,因为您正在通过程序传递它
这就是它的工作原理
Process proc = new Process();
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "cmd";
start.Arguments = "/k";
start.RedirectStandardInput = true;
start.WorkingDirectory = Environment.CurrentDirectory;
start.UseShellExecute = false;
proc.StartInfo = start;
proc.Start();
proc.StandardInput.WriteLine("net user \"username\" password");
非常非常重要!!
以管理员身份启动exe,否则您必须执行此操作
start.UseShellExecute = true;
start.Verb = "runas";
但是,您将无法重定向输出/输入流 我认为您的命令不会等待密码,而是在启动后立即结束。您可以通过手动运行该命令来检查这一点?@Dmitry最后是否输入错误?当我运行调试器并在两个WriteLine
处放置断点时,命令在到达之前没有输入就完成了执行。虽然。我可能无意中向第一个命令发送了输入,断点只是没有对齐。@MasonSchmidgall检查我的解决方案,让我知道它是否有效。我应该提到,该命令一直正确运行,我是以管理员身份运行程序的。但是,我不知道你可以传递密码。我会尽快尝试你的解决方案。我感谢你为我付出的时间和努力!