How to extract a rar file in C#?

2020-04-08 14:27发布

I want to extract .rar files using cmd shell so I wrote this code:

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(); 

The first time it worked fine, the second time it displayed nothing.

9条回答
ゆ 、 Hurt°
2楼-- · 2020-04-08 14:34

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.

查看更多
唯我独甜
3楼-- · 2020-04-08 14:36

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

查看更多
我想做一个坏孩纸
4楼-- · 2020-04-08 14:37

9 Answers, only sam mousavi is answering your question directly, but noone's telling you what's wrong. Citing from the WinRAR manual:

...the command: WinRAR x Fonts *.ttf NewFonts\
will extract *.ttf files from the archive Fonts to the folder NewFonts
You need to use the trailing backslash as in the example above for denoting the destination folder.

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.

查看更多
萌系小妹纸
5楼-- · 2020-04-08 14:38

You can use this lib directly: http://sevenziplib.codeplex.com/

SevenZipLib is a lightweight, easy-to-use managed interface to the 7-zip library.

查看更多
我命由我不由天
6楼-- · 2020-04-08 14:39

We can also use this,

string SourceFile = @"G:\SourceFolder\125.rar";
string DestinationPath = @"G:\Destination\";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = @"G:\Software\WinRAR.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.EnableRaisingEvents = false;            
process.StartInfo.Arguments = string.Format("x -o+ \"{0}\" \"{1}\"", SourceFile, DestinationPath);
process.Start();
查看更多
孤傲高冷的网名
7楼-- · 2020-04-08 14:42
UnRar("C:\\Download\\sampleextractfolder\\", filepath2);

private static void UnRar(string WorkingDirectory, string filepath)
{

    // Microsoft.Win32 and System.Diagnostics namespaces are imported

    //Dim objRegKey As RegistryKey
    RegistryKey objRegKey;
    objRegKey = Registry.ClassesRoot.OpenSubKey("WinRAR\\Shell\\Open\\Command");
    // Windows 7 Registry entry for WinRAR Open Command

    // Dim obj As Object = objRegKey.GetValue("");
    Object obj = objRegKey.GetValue("");

    //Dim objRarPath As String = obj.ToString()
    string objRarPath = obj.ToString();
    objRarPath = objRarPath.Substring(1, objRarPath.Length - 7);

    objRegKey.Close();

    //Dim objArguments As String
    string objArguments;
    // in the following format
    // " X G:\Downloads\samplefile.rar G:\Downloads\sampleextractfolder\"
    objArguments = " X " + " " + filepath + " " + " " + WorkingDirectory;

    // Dim objStartInfo As New ProcessStartInfo()
    ProcessStartInfo objStartInfo = new ProcessStartInfo();

    // Set the UseShellExecute property of StartInfo object to FALSE
    //Otherwise the we can get the following error message
    //The Process object must have the UseShellExecute property set to false in order to use environment variables.
    objStartInfo.UseShellExecute = false;
    objStartInfo.FileName = objRarPath;
    objStartInfo.Arguments = objArguments;
    objStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    objStartInfo.WorkingDirectory = WorkingDirectory + "\\";

    //   Dim objProcess As New Process()
    Process objProcess = new Process();
    objProcess.StartInfo = objStartInfo;
    objProcess.Start();
    objProcess.WaitForExit();


    try
    {
        FileInfo file = new FileInfo(filepath);
        file.Delete();
    }
    catch (FileNotFoundException e)
    {
        throw e;
    }



}
查看更多
登录 后发表回答