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.
Use SevenZipSharp as it's a bit better way of doing things then working with some .exe's.
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";
}));
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With