Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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):
C#
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.

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.
Posted
Updated 24-Feb-12 9:19am
v3

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

p.StartInfo.RedirectStandardInput = true;


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?

C#
StreamWriter myStreamWriter = myProcess.StandardInput;
// Write each line to the StandardInput stream of the command.
string inputText;
myStreamWriter.WriteLine(inputText);
 
Share this answer
 
v3
Comments
Dreamcooled 24-Feb-12 14:56pm    
As I said, I wanna enter the informations via the redirected stream (so I have to set RedirectStandardInput to true).

When I redirect nothing, everything works fine, but that's not the goal (because I wanna hide the window later).
wizardzz 24-Feb-12 15:03pm    
You confused me with this statement; "But I want to provide these informations over the standart input stream!" I'm trying to help, but you don't seem to know how to redirect an input stream. Did you even look at my link? Have you set a StreamWriter to the process's StandardInput?
Dreamcooled 24-Feb-12 15:32pm    
Well I'm sorry for this uncertainty. I checked out your link, and I think I know really well how to redirect a input stream :). But what should I write to the stream (not implemented in the copied source) if the process ends directly after starting? The problem is that the process doesn't wait on my inputs!
wizardzz 24-Feb-12 15:34pm    
So, to clarify, you ARE writing the password to the input stream before calling p.WaitForExit()?
Dreamcooled 24-Feb-12 15:43pm    
No. I start the application and log the output (see question). And I wonder why the application doesn't wait until I enter something (over the input stream), instead it just goes on.

How I want it:
I start the application.
The application asks me for the password.
I provide the password over the input stream.
The application checks the password and goes on (or prints an error).

What happens instead:
I start the application
The application asks me for the password.
(I don't do anything)
The application goes on and prints an error (even if I haven't send any char)! (But it should wait on one!)

(Btw, sorry for my bad english. It's not my mother tongue...)
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).
 
Share this answer
 
Comments
Dave Kreskowiak 27-Feb-12 11:45am    
Too bad I didn't see this post earlier. I would have told you that console password entry schemes don't usually use the StdIn stream of the console but instead check for the keystrokes themselves.

In my experience, only the console commands that echo what you type as you type it in the console work with the StdIn stream. Stuff like arrow keys that move text-drawn menus never work, and password entry doesn't either.
Dreamcooled 27-Feb-12 13:58pm    
Thanks a lot for your answer. Good to know for further projects :).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900