C#流程会跳过输入吗? [英] C# Process skips inputs?

查看:171
本文介绍了C#流程会跳过输入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在使用Unrar时遇到了问题(称为C#进程).
(我知道C#有几种实现,但是大多数实现都是错误的,或者没有我需要的功能.)

一旦我重定向了标准流之一,Unrar就会跳过所有输入(例如密码请求)并退出.但是我想通过(重定向的)标准输入流(p.StandardInput.Write ...)提供这些信息!

有什么问题吗?

来源(简体):

Hi
I''ve a problem with Unrar (called as C# process).
(I know there are several implementations for C#, but the most of them are buggy or they haven''t the functions I need.)

As soon as I redirect one of the standard streams, Unrar skips all inputs (like passwort requests) and exits. But I want to provide these informations over the (redirected) standart input stream (p.StandardInput.Write ...)!

What''s the Problem?

Source (Simplyfied):

Process p = new Process();
p.StartInfo.FileName = "UnRAR.exe";
p.EnableRaisingEvents = true;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);

//p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = "l abc.rar"; 
//List Archive (Filenames encrypted -> Password needed on opening)
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();

p.WaitForExit();

...

void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}



更新:
控制台显示以下内容:



UPDATE:
The Console shows the following:

UNRAR 4.10 freeware      Copyright (c) 1993-2012 Alexander Roshal

Enter password (will not be echoed) for abc.rar: 

Archive abc.rar
  0 files


CRC failed in the encrypted file abc.rar. Corrupt file or wrong password.


但是,正如您在我的源代码中看到的那样,我没有向该进程发送任何内容(因此它应在输入密码"之后等待)
似乎该过程只是跳过了所有getch(或readLine())命令,并使用一个空字符串继续.

感谢您的回答.


But as you see in my source I haven''t send anything to the process (so it should wait after "Enter password" )
It seems as the process simply skips all the getch (or readLine()) commands and takes an empty string to continue.

Thanks for your answers.

推荐答案

我认为您应该重新考虑为什么要这样做:

I think you should reconsider why you are doing this then:

p.StartInfo.RedirectStandardInput = true;



您是否尝试过将其设置为false?


更新:

http://msdn.microsoft.com/en-us/library/system. diagnostics.processstartinfo.redirectstandardinput.aspx [ ^ ]

按照此处的示例,您是否在代码中的任何位置执行此操作?



Have you tried setting to false?


Update:

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx[^]

Following the example here, are you doing this anywhere in your code?

StreamWriter myStreamWriter = myProcess.StandardInput;
// Write each line to the StandardInput stream of the command.
string inputText;
myStreamWriter.WriteLine(inputText);


好吧,我无法解决问题. 7zip命令行工具也有类似的问题.
我已经为Unrar.dll修改了提供的C#包装程序,可以从他们的身边下载该文件.好像我可以根据需要自定义包装程序(作为问题的解决方法).
Well, I couldn''t solve the Problem. The 7zip commandline tool has similar issues.
I''ve modified the provided C# Wrapper for the Unrar.dll, which can be found in the download from their side. It seems as if I could customize the wrapper to my needs (as a Workaround for the Problem).


这篇关于C#流程会跳过输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆