gpt4 book ai didi

c# - 如何在 C# 中提取 rar 文件?

转载 作者:太空狗 更新时间:2023-10-30 00:17:13 25 4
gpt4 key购买 nike

我想使用 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();

第一次没问题,第二次就什么都没有了。

最佳答案

使用SevenZipSharp因为与使用某些 .exe 文件相比,这是一种更好的做事方式。

private ReadOnlyCollection<string> ExtractArchive(string varPathToFile, string varDestinationDirectory) {
ReadOnlyCollection<string> readOnlyArchiveFilenames;
ReadOnlyCollection<string> readOnlyVolumeFilenames;
varExtractionFinished = false;
varExtractionFailed = false;
SevenZipExtractor.SetLibraryPath(sevenZipDll);
string fileName = "";
string directory = "";
Invoke(new SetNoArgsDelegate(() => {
fileName = varPathToFile;
directory = varDestinationDirectory;
}));
using (SevenZipExtractor extr = new SevenZipExtractor(fileName)) {
//string[] test = extr.ArchiveFileNames.

readOnlyArchiveFilenames = extr.ArchiveFileNames;
readOnlyVolumeFilenames = extr.VolumeFileNames;
//foreach (string dinosaur in readOnlyDinosaurs) {
//MessageBox.Show(dinosaur);
// }
//foreach (string dinosaur in readOnlyDinosaurs1) {
// // MessageBox.Show(dinosaur);
// }
try {
extr.Extracting += extr_Extracting;
extr.FileExtractionStarted += extr_FileExtractionStarted;
extr.FileExists += extr_FileExists;
extr.ExtractionFinished += extr_ExtractionFinished;

extr.ExtractArchive(directory);
} catch (FileNotFoundException error) {
if (varExtractionCancel) {
LogBoxTextAdd("[EXTRACTION WAS CANCELED]");
} else {
MessageBox.Show(error.ToString(), "Error with extraction");
varExtractionFailed = true;
}
}
}
varExtractionFinished = true;
return readOnlyVolumeFilenames;
}

private void extr_FileExists(object sender, FileOverwriteEventArgs e) {
listViewLogFile.Invoke(new SetOverwriteDelegate((args) => LogBoxTextAdd(String.Format("Warning: \"{0}\" already exists; overwritten\r\n", args.FileName))), e);
}
private void extr_FileExtractionStarted(object sender, FileInfoEventArgs e) {
listViewLogFile.Invoke(new SetInfoDelegate((args) => LogBoxTextAdd(String.Format("Extracting \"{0}\"", args.FileInfo.FileName))), e);
}
private void extr_Extracting(object sender, ProgressEventArgs e) {
progressBarCurrentExtract.Invoke(new SetProgressDelegate((args) => progressBarCurrentExtract.Increment(args.PercentDelta)), e);
}
private void extr_ExtractionFinished(object sender, EventArgs e) {
Invoke(new SetNoArgsDelegate(() => {
//pb_ExtractWork.Style = ProgressBarStyle.Blocks;
progressBarCurrentExtract.Value = 0;
varExtractionFinished = true;
//l_ExtractProgress.Text = "Finished";
}));
}

当然你需要稍微调整一下,使用一些你自己的东西。但为了举例,我添加了一些额外的方法。

关于c# - 如何在 C# 中提取 rar 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3238173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com