如何在C#中提取rar文件?
我想使用 cmd shell 提取 .rar 文件,所以我编写了以下代码:
string commandLine = @"c:\progra~1\winrar\winrar e c:\download\TestedU.rar c:\download";
ProcessStartInfo PSI = new ProcessStartInfo("cmd.exe");
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
Process p = Process.Start(PSI);
StreamWriter SW = p.StandardInput;
StreamReader SR = p.StandardOutput;
SW.WriteLine(commandLine);
SW.Close();
第一次运行正常,第二次则什么也不显示。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 SevenZipSharp 因为它比使用某些 .exe 更好。
当然,你需要稍微调整一下,并使用一些你自己的东西。但为了举例,我添加了一些额外的方法。
Use SevenZipSharp as it's a bit better way of doing things then working with some .exe's.
Of course you need to adjust things a bit, and use some of your own stuff. But for the sake of example I've added some additional methods.
您可以跳过中间步骤并直接使用参数调用 winrar.exe,而不是首先实例化 cmd.exe
另外,您也可以查看 7-zip SDK
You might skip the middle step and call the winrar.exe with the parameters straight instead of first instanciating cmd.exe
Also you might take a look at the 7-zip SDK
您忘记添加读取错误的流。如果 WINRAR 运行正常,当您添加流来读取它时,您会发现错误输出。
You forgot to add a stream for reading errors. If WINRAR is behaving properly, you will find your error output when you add the stream to read it.
我得到了答案。
试试这个:
I Got the answer.
try this one:
正如 Kjartan 所建议的,根据您的用途,使用 7-Zip SDK 可能比生成外部可执行文件更好:
7-Zip SDK 是一个 C/C++ 库,但 http://sevenzipsharp.codeplex.com/ 有一个围绕 7-Zip SDK 的 .Net 库,这使得在 .NET 中使用更容易。
As Kjartan suggested, using 7-Zip SDK may be a better option than spawning an external executable depending on your use:
7-Zip SDK is a C/C++ library but http://sevenzipsharp.codeplex.com/ has a .Net library of it around the 7-Zip SDK which makes it easier to use in .NET.
我们也可以用这个,
We can also use this,
9 个答案,只有 sam mousavi 直接回答你的问题,但没有人告诉你出了什么问题。引用WinRAR手册:
而这正是
c:\download
中缺少的内容。现在,它尝试将存档内的文件 c:\download 提取到当前目录。它第一次如何运作仍然是个谜。9 Answers, only sam mousavi is answering your question directly, but noone's telling you what's wrong. Citing from the WinRAR manual:
And that's exactly what's missing up there at
c:\download
. Right now it tries to extract the file c:\download inside the archive to the current directory. How it could work the first time is a mystery.您可以直接使用此库: http://sevenziplib.codeplex.com/
You can use this lib directly: http://sevenziplib.codeplex.com/