在 C# 中以编程方式响应 cmd 提示符

发布于 2024-11-08 16:07:01 字数 1624 浏览 0 评论 0原文

我试图将命令发送到正在等待我输入的命令提示符。

有问题的命令是 JavaLoader,如果我尝试在带有密码的 BlackBerry 设备上使用该命令,它会提示我输入密码。我想输入密码,但使用 C# 代码。

假设我已经使用如下所示的进程成功创建了 cmd.exe:

process = new Process();

// Invokes the cmd process specifying the command to be executed.
string cmdProcess = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\cmd.exe", new object[] { Environment.SystemDirectory });

// Pass executing file to cmd (Windows command interpreter) as a arguments
//Removed /C tells cmd that we want it to execute the command that follows, and then exit.
string cmdArguments = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", new object[] { command });

// Pass any command line parameters for execution
if (arguments != null && arguments.Length > 0)
{
    cmdArguments += string.Format(System.Globalization.CultureInfo.InvariantCulture, " {0}", new object[] { arguments, System.Globalization.CultureInfo.InvariantCulture });
}

process.StartInfo.FileName = cmdProcess;
process.StartInfo.Arguments = cmdArguments;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;  // Must be false for redirection
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;

bool s = process.Start();

How do I go aboutResponse the cmd's提示输入密码?

我尝试获取输入流并使用 .WriteLine("randompassword") ,如下所示:

while (process.Responding)
{
   sw.WriteLine(response);
}

我注意到的另一件事是密码提示没有被拾取为输出或错误,并且应用程序实际上会挂在这里,因为它正在等待我的输入。

I'm stuck trying to send commands to a cmd prompt that is waiting for my input.

The command in question is JavaLoader, which if I try to use on a BlackBerry device with a password it prompts me for a password. I would like to enter a password but using C# code.

Assuming that I've successfully created the cmd.exe using a Process like so:

process = new Process();

// Invokes the cmd process specifying the command to be executed.
string cmdProcess = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\cmd.exe", new object[] { Environment.SystemDirectory });

// Pass executing file to cmd (Windows command interpreter) as a arguments
//Removed /C tells cmd that we want it to execute the command that follows, and then exit.
string cmdArguments = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", new object[] { command });

// Pass any command line parameters for execution
if (arguments != null && arguments.Length > 0)
{
    cmdArguments += string.Format(System.Globalization.CultureInfo.InvariantCulture, " {0}", new object[] { arguments, System.Globalization.CultureInfo.InvariantCulture });
}

process.StartInfo.FileName = cmdProcess;
process.StartInfo.Arguments = cmdArguments;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;  // Must be false for redirection
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;

bool s = process.Start();

How do I go about responding the cmd's prompt for a passwords?

I've tried getting the Input stream and using .WriteLine("randompassword") like so:

while (process.Responding)
{
   sw.WriteLine(response);
}

Another thing I've noticed is the prompt for a password is not being picked up as Output or Error and the application will in fact hang here because it's waiting for my input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文