[英]How use cmd command in c# program?
I want cmd command in my c# winforms program. 我想在我的C#Winforms程序中使用cmd命令。 I want use command to compress file with 7zip.
我想使用命令使用7zip压缩文件。
In normal cmd i use cd "C:\\Program Files(x86)\\7-Zip" & 7z.exe a -tzip "C:\\xyz\\test\\txt" "C:\\xyz\\test\\txt" -m0=BZip2 -mx5
在正常的cmd中,我使用
cd "C:\\Program Files(x86)\\7-Zip" & 7z.exe a -tzip "C:\\xyz\\test\\txt" "C:\\xyz\\test\\txt" -m0=BZip2 -mx5
string zip = @"/c C:\Program Files(x86)\7-Zip";
string file = @"/c C:\xyz\txt";
string conv = "/c & 7z.exe a -tzip";
string method = "/c -m0=BZip2 -mx5";
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = @"C:\windows\system32\cmd.exe";
proc.Arguments = @"/c cd" + zip + conv + file + file + method;
System.Diagnostics.Process.Start(proc);
Doesn't work my code. 我的代码不起作用。 How can i use this command in my program.
我如何在程序中使用此命令。 I want compress file when i click in button
我单击按钮时要压缩文件
Something like this: 像这样:
// File (exe) to start: combination of folder and exe
string fileName = Path.Combine(
// Let's not hardcode "C:\Program Files(x86)" and alike
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
@"7-Zip",
@"7z.exe");
// If desired arguments are
// a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
// we can join them as
string arguments = string.Join(" ",
@"a",
@"-tzip", //TODO: you may want to have these values
@"""C:\xyz\test\txt""", // as variables like file, conv, method etc.
@"""C:\xyz\test\txt""",
@"-m0=BZip2",
@"-mx5");
ProcessStartInfo procInfo = new ProcessStartInfo() {
FileName = fileName, // We want to start fileName
Arguments = arguments, // With arguments
}
// Process is IDisposable, do not forget to Dispose HProcess handle
using (var process = Process.Start(procInfo)) {
// It's fire and forget implementation, if you want to wait for results
// add process.WaitForExit();
// process.WaitForExit(); // uncomment, if you want to pause
}
You seem to have made a mistake with your arguments. 您的论点似乎犯了一个错误。
Your current command line will be: 您当前的命令行为:
C:\windows\system32\cmd.exe /c cd/c C:\Program Files(x86)\7-Zip/c & 7z.exe a -tzip/c C:\xyz\txt/c C:\xyz\txt/c -m0=BZip2 -mx5
The spurious /c
everywhere is not going to help and neither are the lack of spaces. 到处都是虚假的
/c
不会有所帮助,缺少空格也不会有帮助。
I'm guessing what you meant to run was these two commands in succession: 我猜您要连续运行的是以下两个命令:
cd C:\Program Files(x86)\7-Zip
7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5
Which means you should change your code to: 这意味着您应该将代码更改为:
string zip = @" C:\Program Files(x86)\7-Zip";
string file = @" C:\xyz\txt";
string conv = " & 7z.exe a -tzip";
string method = " -m0=BZip2 -mx5";
This will produce: 这将产生:
C:\windows\system32\cmd.exe /c cd C:\Program Files(x86)\7-Zip & 7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.