带有 WinRAR 的 C# 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1424673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:23:34  来源:igfitidea点击:

C# program with WinRAR

c#

提问by user147685

I want to design a c# program which can run program a 3rd exe application such as WinRAR. the program will browse for file and when then user click a button, the process to create archive will begin..!

我想设计一个 ac# 程序,它可以运行第三个 exe 应用程序,例如 WinRAR。程序将浏览文件,然后当用户单击按钮时,创建存档的过程将开始..!

I know using System.Diagnostics.Process.Start method can execute the .exe file. for eg.

我知道使用 System.Diagnostics.Process.Start 方法可以执行 .exe 文件。例如。

Process.Start(@"C:\path\to\file.zip");

GetFile("filename","open winrar to execute the file") I need something like this. I wanna pass the file name to that 3rd application, without need to open winrar. Is it possible? How should i start? Any references/guidance/solution are very thankful.

GetFile("filename","open winrar to execute the file") 我需要这样的东西。我想将文件名传递给第三个应用程序,而无需打开 winrar。是否可以?我应该如何开始?任何参考/指导/解决方案都非常感谢。

thank you very much.

非常感谢您。

//UPDATED

//更新

Here is the code to open the WinRAR.exe program else the error message appeared.I pun it in button_click and accept file from txtDest.text using browse. So from here, instead of open the files, i want to to compress directly. I try to change "RAR.exe" or "UNRAR.exe" but it didnt work. It is right?

这是打开 WinRAR.exe 程序的代码,否则会出现错误消息。我将它放在 button_click 中并使用浏览从 txtDest.text 接受文件。所以从这里开始,我想直接压缩而不是打开文件。我尝试更改“RAR.exe”或“UNRAR.exe”,但没有奏效。这是正确的?

thank you.

谢谢你。

 ProcessStartInfo startInfo = new ProcessStartInfo("WinRAR.exe");
 startInfo.WindowStyle = ProcessWindowStyle.Maximized;
 startInfo.Arguments = txtDest.Text;
 try
 {
   // Start the process with the info we specified.
   using (Process exeProcess = Process.Start(startInfo))
    {
        exeProcess.WaitForExit();
    }
  }
  catch
    {
        MessageBox.Show("Error Open");
    }
  }

采纳答案by Dale

For this you probably want to use unrar.dllwhich is the library distributed by RarLabs, the people who make Winrar. It contains all the functionality of WinRAR exposed as a COM interface. I used it recently in a project and it is quite good, exposes methods for opening and browsing archives, as well as compression and decompression.

为此,您可能想要使用unrar.dll,它是由制作 Winrar 的 RarLabs 分发的库。它包含作为 COM 接口公开的 WinRAR 的所有功能。我最近在一个项目中使用了它,它非常好,公开了打开和浏览档案的方法,以及压缩和解压。

http://www.rarlab.com/rar_add.htmscroll down to "UnRAR.dll UnRAR dynamic library for Windows software developers."

http://www.rarlab.com/rar_add.htm向下滚动到“Windows 软件开发人员的 UnRAR.dll UnRAR 动态库”。

It comes with a really good set of examples including browsing an archive and API documentation.

它附带了一组非常好的示例,包括浏览存档和 API 文档。

回答by Joshua

Its possible if WinRar accepts command line arguments which it almost certainly does.
Hereis a good example of how to do this sort or thing, number 4 is closest to what you want I think.

如果 WinRar 接受它几乎肯定会接受的命令行参数,则有可能。
是一个很好的例子,说明如何做这种事情,数字 4 最接近我认为的你想要的。

回答by Noon Silk

Start with ProcessStartInfo. There are some good examples there that I would only be repeating here. You may be interested in capturing the output of the program that you execute though, so in that case you'll need to set

ProcessStartInfo开始。那里有一些很好的例子,我只会在这里重复。您可能对捕获您执行的程序的输出感兴趣,因此在这种情况下,您需要设置

psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

Then you may read them from the resulting 'Process' object:

然后您可以从生成的“ Process”对象中读取它们:

string s = process.StandardOutput.ReadToEnd();

回答by Havenard

WinRAR installation brings its console versions, "RAR.EXE" and "UNRAR.EXE". As console applications made to be fully controlled via command line. You integrate it by preparing the command line and starting the process, optionally hidden so user won't see anything but your program.

WinRAR 安装带来了它的控制台版本,“RAR.EXE”和“UNRAR.EXE”。由于控制台应用程序可以通过命令行完全控制。您可以通过准备命令行并启动进程来集成它,可选择隐藏,以便用户除了您的程序之外看不到任何东西。

Usage:

用法:

RAR.EXE a "C:\file to create.rar" "C:\Documents\file to include.jpg"         
"C:\Photos\my picture.jpg" "C:\curriculum.doc" [... as many files you want]

I believe there is a "rarLib" somewhere to procure it more professionally.

我相信有一个“rarLib”可以更专业地获得它。

回答by Stefan Steiger

What about this one:
http://nunrar.codeplex.com/

这个怎么样:http:
//nunrar.codeplex.com/

回答by Wolfzoon

Yes, I'm ressurrecting a completely dead question here, but I've not seen anyone put up the exact answer you(and until 20 minutes ago I too) want, so let me put 2 and 2 together:

是的,我在这里复活了一个完全死的问题,但我还没有看到有人提出你(直到 20 分钟前我也是)想要的确切答案,所以让我把 2 和 2 放在一起:

Command line Usage: rar.exe a <target .rar file> <file to rar> {<more files>}
You can make more complicated names, like ones containing spaces, by putting quotation marks around the names. The program you'll probably want is thus:

命令行用法:rar.exe a <target .rar file> <file to rar> {<more files>}
您可以通过在名称周围加上引号来创建更复杂的名称,例如包含空格的名称。您可能想要的程序是这样的:

string targetArchiveName = "archive.rar",
targetFile = "testFile.txt";
ProcessStartInfo startInfo = new ProcessStartInfo("WinRAR.exe");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = string.Format("a \"{0}\" \"{1}\"",
                      targetArchiveName, targetFile);
try
{
  // Start the process with the info we specified.
  using (Process exeProcess = Process.Start(startInfo))
  {
    exeProcess.WaitForExit();
  }
}
catch
{
  {
    MessageBox.Show("Error Open");
  }
}