如何在C#中提取rar文件?我想使用cmdshell提取.rar文件,所以我写了这段代码:stringcommandLine=@"c:progra~1winrarwinrarec:downloadTestedU.rarc:download";ProcessStartInfoPSI=newProcessStartInfo("cmd.exe");PSI.RedirectStandardInput=true;PSI.RedirectStandardOutput=true;PSI.RedirectStandardError=true;PSI.UseShellExecute=false;进程p=Process.Start(PSI);.WriteLine(命令行);SW.关闭();第一次没问题,第二次就什么都没有了。使用SevenZipSharp,因为它比使用某些.exe更好。privateReadOnlyCollectionExtractArchive(stringvarPathToFile,stringvarDestinationDirectory){ReadOnlyCollectionreadOnlyArchiveFilenames;ReadOnlyCollectionreadOnlyVolumeFilenames;varExtractionFinished=false;varExtractionFailed=false;SevenZipExtractor.SetLibraryPath(sevenZipDll);字符串文件名="";字符串目录=“”;调用(newSetNoArgsDelegate(()=>{fileName=varPathToFile;directory=varDestinationDirectory;}));使用(SevenZipExtractorextr=newSevenZipExtractor(fileName)){//string[]test=extr.ArchiveFileNames.readOnlyArchiveFilenames=extr.ArchiveFileNames;readOnlyVolumeFilenames=extr.VolumeFileNames;//foreach(readOnlyDinosaurs中的字符串恐龙){//MessageBox.Show(dinosaur);//}//foreach(readOnlyDinosaurs1中的字符串恐龙){////MessageBox.Show(dinosaur);//}try{extr.Extracting+=extr_Extracting;extr.FileExtractionStarted+=extr_FileExtractionStarted;extr.FileExists+=extr_FileExist文件存在;extr.ExtractionFinished+=extr_ExtractionFinished;extr.ExtractArchive(目录);}catch(FileNotFoundExceptionerror){if(varExtractionCancel){LogBoxTextAdd("[EXTRACTIONWASCANCELED]");}else{MessageBox.Show(error.ToString(),"提取错误");varExtractionFailed=true;}}}varExtractionFinished=true;返回readOnlyVolumeFilenames;}privatevoidextr_FileExists(objectsender,FileOverwriteEventArgse){listViewLogFile.Invoke(newSetOverwriteDelegate((args)=>LogBoxTextAdd(String.Format("Warning:"{0}"alreadyexists;overwrittenrn",args.FileName))),e);}privatevoidextr_FileExtractionStarted(objectsender,FileInfoEventArgse){listViewLogFile.Invoke(newSetInfoDelegate((args)=>LogBoxTextAdd(String.Format("Extracting"{0}"",args.FileInfo.FileName))),e);}privatevoidextr_Extracting(objectsender,ProgressEventArgse){progressBarCurrentExtract.Invoke(newSetProgressDelegate((args)=>progressBarCurrentExtract.Increment(args.PercentDelta)),e);}privatevoidextr_ExtractionFinished(objectsender,EventArgse){Invoke(newSetNoArgsDelegate(()=>{//pb_ExtractWork.Style=ProgressBarStyle.Blocks;progressBarCurrentExtract.Value=0;varExtractionFinished=true;//l_ExtractProgress.Text="Finished";}));}当然你需要稍微调整一下并使用你自己的东西,但为了举例,我添加了一些额外的方法。可以跳过中间步骤,直接带参数调用winrar.exe,不用先实例化cmd.exe也可以看看7-zipSDKUnRar("C:\Download\sampleextractfolder\",filepath2);privatestaticvoidUnRar(stringWorkingDirectory,stringfilepath){//导入Microsoft.Win32和System.Diagnostics命名空间//DimobjRegKeyAsRegistryKeyRegistryKeyobjRegKey;objRegKey=Registry.ClassesRoot.OpenSubKey("WinRAR\Shell\Open\Command");//WinRAR打开命令的Windows7注册表项//DimobjAsObject=objRegKey.GetValue("");对象obj=objRegKey.GetValue("");//DimobjRarPathAsString=obj.ToString()stringobjRarPath=obj.字符串();objRarPath=objRarPath.Substring(1,objRarPath.Length-7);objRegKey.Close();//DimobjArgumentsAsStringstringobjArguments;//格式如下//"XG:Downloadssamplefile.rarG:Downloadssampleextractfolder"objArguments="X"+""+filepath+""+""+WorkingDirectory;//DimobjStartInfoAsNewProcessStartInfo()ProcessStartInfoobjStartInfo=newProcessStartInfo();//将StartInfo对象的UseShellExecute属性设置为FALSE//否则我们会收到以下错误消息//Process对象必须将UseShellExecute属性设置为false才能使用环境变量.objStartInfo.UseShellExecute=false;objStartInfo.FileName=objRarPath;objStartInfo.Arguments=objArguments;objStartInfo.WindowStyle=ProcessWindowStyle.Hidden;objStartInfo.WorkingDirectory=WorkingDirectory+"\";//DimobjProcProcessAsNewProcess();objProcess.StartInfo=objStartInfo;objProcess.Start();objProcess.WaitForExit();尝试{FileInfo文件=newFileInfo(文件路径);文件.删除();}catch(FileNotFoundExceptione){抛出e;你忘记添加流来读取错误如果WINRAR运行良好,你会发现添加流来读取时错误输出。我有答案。试试这个:System.Diagnostics.Processproc=newSystem.Diagnostics.Process();//放入安装的winrar.exe的路径proc.StartInfo.FileName=@"C:ProgramFilesWinRARunrar.exe";proc.StartInfo.创建无窗口=真;proc.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;proc.EnableRaisingEvents=true;//PWD:如果文件有任何密码//SRC:rar文件的路径。eg:c:tempabc.rar//DES:你希望它被提取的路径。例如:d:extracted//注意:目标文件夹必须存在!proc.StartInfo.Arguments=String.Format("x-p{0}{1}{2}",PWD,SRC,DES);proc.开始();正如Kjartan所建议的那样,使用7-ZipSDK可能比生成外部可执行文件更好,具体取决于您的用途:7-ZipSDK是一个C/C++库,但是http://sevenzipsharp.codeplex.com/有一个围绕7-ZipSDK的.Net库,使其更易于在.NET中使用。我们也可以使用这个,stringSourceFile=@"G:SourceFolder125.rar";stringDestinationPath=@"G:Destination";System.Diagnostics.Processprocess=newSystem.Diagnostics.Process();process.StartInfo.FileName=@"G:SoftwareWinRAR.exe";process.StartInfo.CreateNoWindow=true;process.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;process.EnableRaisingEvents=false;process.StartInfo.Arguments=string.Format("x-o+"{0}""{1}"",SourceFile,DestinationPath);过程。开始();9个答案,只有sammousavi直接回答了你的问题,但没有人告诉你哪里出了问题。引用WinRAR手册:...命令:WinRARxFonts*.ttfNewFonts将*.ttf文件从存档字体提取到文件夹NewFonts您需要在上面的示例中使用尾部反斜杠来表示目标文件夹。这正是c:download所缺乏的。现在它尝试将存档中的文件c:download提取到当前目录。它最初是如何工作的是一个谜。您可以直接使用这个库:http://sevenziplib.codeplex.com/SevenZipLib是一个轻量级、易于使用的7-zip库管理界面。以上就是C#学习教程:如何在C#中提取rar文件?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
