积极答复者
关于C#调用WinRar.exe命令行进行解压缩的问题

问题
-
我有个C#中可执行压缩的程序如下:
/// <summary>
/// 利用 WinRAR 进行压缩
/// </summary>
/// <param name="rarName">压缩文件的名称(包括后缀,不写后缀默认为.rar)</param>
/// <param name="path">将要被压缩的文件夹(绝对路径)</param>
/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
public static bool RAR(string rarName, string path)
{
bool flag = false;
string rarexe; //WinRAR.exe 的完整路径
RegistryKey regkey; //注册表键
Object regvalue; //键值
string cmd; //WinRAR 命令参数
//ProcessStartInfo startinfo;
//Process process;
try
{
//regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe\");
regvalue = regkey.GetValue(""); // 键值为 "d:\Program Files\WinRAR\WinRAR.exe" "%1"
rarexe = regvalue.ToString();
regkey.Close();
//rarexe = rarexe.Substring(1, rarexe.Length - 7); // d:\Program Files\WinRAR\WinRAR.exe
rarexe = Path.GetFileName(rarexe);
//Directory.CreateDirectory(path);
//压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
//最终EP1才是我想要的效果,因此合理的处理用EP1参数(此命令表示把Desktop222目录(文件夹)下的所有文件,压缩为Desktop333.rar压缩文件):
//RAR A -ibck E:\临时\测试解密非cs文件\Desktop333 -EP1 E:\临时\测试解密非cs文件\Desktop222
//cmd = string.Format("a {0} {1} -r",
// rarName,
// path);
cmd = string.Format(" A {0} -EP1 {1}",
rarName,
path);
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "RAR.exe";//设定需要执行的命令 cmd.exe,采用RAR.exe 不会被亿赛通加密,如果采用WinRAR.exe会被亿赛通加密,虽然两者都可,但是还是采用前者,可以绕过亿赛通
//如果要复制的路径带有空格、&等特殊符号的时候,请使用引号把这些特殊字符括起来,比如: copy "d:\test abc\test.txt" "d:\t&est"
startInfo.Arguments = cmd;//“/C”表示执行完命令后马上退出 (这个里面是exe程序的执行参数,切记不包含exe程序名称)
//startInfo.UseShellExecute = false;//不使用系统外壳程序启动 (当执行的是cmd外部的exe程序时,需要把此属性设置注释掉)
startInfo.WorkingDirectory = Path.GetDirectoryName(regvalue.ToString());
startInfo.RedirectStandardInput = false;//不重定向输入
//startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(); //无限期等待进程 winrar.exe 退出
if (process.HasExited)
{
flag = true;
}
process.Close();
process.Dispose();
}
catch (Exception e)
{
throw e;
}
return flag;
}但是问题就来了,
rar进行压缩时,为了保证文件所在的目录不会添加进去,那么,必须把文件夹里面的所有的文件,分别都要调用压缩程序进行处理,而不是一起执行。比如说,一个AA文件夹里面有两个文件分别为①文件和②文件,那么如果直接对AA文件夹进行压缩的话,会导致压缩包里面首先是AA文件夹这个目录,双击目录后,才看到对应的两个文件,但其实应该是压缩包里面直接就是那两个文件才对
那么像我说的这样来压缩文件,如何解决呢?rar的命令该如何写呢?求大神传道授业解惑!
答案
-
Hi,
你要的效果是WINRAR压缩文件不压缩路径的操作把
加上-ep
full code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private static string pathExe = @"D:\WinRAR.exe"; public static bool GZipFile(string sourcefilename, string zipfilename) { bool isSucc = false; //拼接压缩命令参数 string args = string.Format("a -ep {0} {1}", zipfilename, sourcefilename); //启动压缩进程 isSucc = ProcessHelper.StartProcess(pathExe, args); return isSucc; } private void button1_Click(object sender, EventArgs e) { GZipFile(@"D:\A", @"D:\test.zip"); } } public class ProcessHelper { /// <summary> /// 启动进程执行exe /// </summary> /// <param name="exePath">exe路径</param> /// <param name="exeArgs">exe所需参数</param> /// <returns></returns> public static bool StartProcess(string exePath, string exeArgs) { bool isHidden = true; bool isSucc = true; Process process = new Process(); process.StartInfo.FileName = exePath; process.StartInfo.Arguments = exeArgs; if (isHidden) { process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.CreateNoWindow = true; } else { process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; process.StartInfo.CreateNoWindow = false; } process.Start(); int idx = 1; while (!process.HasExited) { idx++; process.WaitForExit(1000); if (idx == 50) { process.Kill(); isSucc = false; } } process.Close(); process.Dispose(); return isSucc; } } }
Best Regards,
Alex
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已编辑 Alex-KSGZ 2019年5月31日 9:17
- 已标记为答案 TheYangtzeTheYellowRiver 2019年6月1日 2:44
全部回复
-
我有个C#中可执行压缩的程序如下:
/// <summary>
/// 利用 WinRAR 进行压缩
/// </summary>
/// <param name="rarName">压缩文件的名称(包括后缀,不写后缀默认为.rar)</param>
/// <param name="path">将要被压缩的文件夹(绝对路径)</param>
/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
public static bool RAR(string rarName, string path)
{
bool flag = false;
string rarexe; //WinRAR.exe 的完整路径
RegistryKey regkey; //注册表键
Object regvalue; //键值
string cmd; //WinRAR 命令参数
//ProcessStartInfo startinfo;
//Process process;
try
{
//regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe\");
regvalue = regkey.GetValue(""); // 键值为 "d:\Program Files\WinRAR\WinRAR.exe" "%1"
rarexe = regvalue.ToString();
regkey.Close();
//rarexe = rarexe.Substring(1, rarexe.Length - 7); // d:\Program Files\WinRAR\WinRAR.exe
rarexe = Path.GetFileName(rarexe);
//Directory.CreateDirectory(path);
//压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
//最终EP1才是我想要的效果,因此合理的处理用EP1参数(此命令表示把Desktop222目录(文件夹)下的所有文件,压缩为Desktop333.rar压缩文件):
//RAR A -ibck E:\临时\测试解密非cs文件\Desktop333 -EP1 E:\临时\测试解密非cs文件\Desktop222
//cmd = string.Format("a {0} {1} -r",
// rarName,
// path);
cmd = string.Format(" A {0} -EP1 {1}",
rarName,
path);
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "RAR.exe";//设定需要执行的命令 cmd.exe,采用RAR.exe 不会被亿赛通加密,如果采用WinRAR.exe会被亿赛通加密,虽然两者都可,但是还是采用前者,可以绕过亿赛通
//如果要复制的路径带有空格、&等特殊符号的时候,请使用引号把这些特殊字符括起来,比如: copy "d:\test abc\test.txt" "d:\t&est"
startInfo.Arguments = cmd;//“/C”表示执行完命令后马上退出 (这个里面是exe程序的执行参数,切记不包含exe程序名称)
//startInfo.UseShellExecute = false;//不使用系统外壳程序启动 (当执行的是cmd外部的exe程序时,需要把此属性设置注释掉)
startInfo.WorkingDirectory = Path.GetDirectoryName(regvalue.ToString());
startInfo.RedirectStandardInput = false;//不重定向输入
//startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(); //无限期等待进程 winrar.exe 退出
if (process.HasExited)
{
flag = true;
}
process.Close();
process.Dispose();
}
catch (Exception e)
{
throw e;
}
return flag;
}但是问题就来了,
rar进行压缩时,为了保证文件所在的目录不会添加进去,那么,必须把文件夹里面的所有的文件,分别都要调用压缩程序进行处理,而不是一起执行。比如说,一个AA文件夹里面有两个文件分别为①文件和②文件,那么如果直接对AA文件夹进行压缩的话,会导致压缩包里面首先是AA文件夹这个目录,双击目录后,才看到对应的两个文件,但其实应该是压缩包里面直接就是那两个文件才对
那么像我说的这样来压缩文件,如何解决呢?rar的命令该如何写呢?求大神传道授业解惑!
-
我有个C#中可执行压缩的程序如下:
/// <summary>
/// 利用 WinRAR 进行压缩
/// </summary>
/// <param name="rarName">压缩文件的名称(包括后缀,不写后缀默认为.rar)</param>
/// <param name="path">将要被压缩的文件夹(绝对路径)</param>
/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
public static bool RAR(string rarName, string path)
{
bool flag = false;
string rarexe; //WinRAR.exe 的完整路径
RegistryKey regkey; //注册表键
Object regvalue; //键值
string cmd; //WinRAR 命令参数
//ProcessStartInfo startinfo;
//Process process;
try
{
//regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe\");
regvalue = regkey.GetValue(""); // 键值为 "d:\Program Files\WinRAR\WinRAR.exe" "%1"
rarexe = regvalue.ToString();
regkey.Close();
//rarexe = rarexe.Substring(1, rarexe.Length - 7); // d:\Program Files\WinRAR\WinRAR.exe
rarexe = Path.GetFileName(rarexe);
//Directory.CreateDirectory(path);
//压缩命令,相当于在要压缩的文件夹(path)上点右键->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
//最终EP1才是我想要的效果,因此合理的处理用EP1参数(此命令表示把Desktop222目录(文件夹)下的所有文件,压缩为Desktop333.rar压缩文件):
//RAR A -ibck E:\临时\测试解密非cs文件\Desktop333 -EP1 E:\临时\测试解密非cs文件\Desktop222
//cmd = string.Format("a {0} {1} -r",
// rarName,
// path);
cmd = string.Format(" A {0} -EP1 {1}",
rarName,
path);
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "RAR.exe";//设定需要执行的命令 cmd.exe,采用RAR.exe 不会被亿赛通加密,如果采用WinRAR.exe会被亿赛通加密,虽然两者都可,但是还是采用前者,可以绕过亿赛通
//如果要复制的路径带有空格、&等特殊符号的时候,请使用引号把这些特殊字符括起来,比如: copy "d:\test abc\test.txt" "d:\t&est"
startInfo.Arguments = cmd;//“/C”表示执行完命令后马上退出 (这个里面是exe程序的执行参数,切记不包含exe程序名称)
//startInfo.UseShellExecute = false;//不使用系统外壳程序启动 (当执行的是cmd外部的exe程序时,需要把此属性设置注释掉)
startInfo.WorkingDirectory = Path.GetDirectoryName(regvalue.ToString());
startInfo.RedirectStandardInput = false;//不重定向输入
//startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(); //无限期等待进程 winrar.exe 退出
if (process.HasExited)
{
flag = true;
}
process.Close();
process.Dispose();
}
catch (Exception e)
{
throw e;
}
return flag;
}但是问题就来了,
rar进行压缩时,为了保证文件所在的目录不会添加进去,那么,必须把文件夹里面的所有的文件,分别都要调用压缩程序进行处理,而不是一起执行。比如说,一个AA文件夹里面有两个文件分别为①文件和②文件,那么如果直接对AA文件夹进行压缩的话,会导致压缩包里面首先是AA文件夹这个目录,双击目录后,才看到对应的两个文件,但其实应该是压缩包里面直接就是那两个文件才对
那么像我说的这样来压缩文件,如何解决呢?rar的命令该如何写呢?求大神传道授业解惑!
-
Hi,
你要的效果是WINRAR压缩文件不压缩路径的操作把
加上-ep
full code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private static string pathExe = @"D:\WinRAR.exe"; public static bool GZipFile(string sourcefilename, string zipfilename) { bool isSucc = false; //拼接压缩命令参数 string args = string.Format("a -ep {0} {1}", zipfilename, sourcefilename); //启动压缩进程 isSucc = ProcessHelper.StartProcess(pathExe, args); return isSucc; } private void button1_Click(object sender, EventArgs e) { GZipFile(@"D:\A", @"D:\test.zip"); } } public class ProcessHelper { /// <summary> /// 启动进程执行exe /// </summary> /// <param name="exePath">exe路径</param> /// <param name="exeArgs">exe所需参数</param> /// <returns></returns> public static bool StartProcess(string exePath, string exeArgs) { bool isHidden = true; bool isSucc = true; Process process = new Process(); process.StartInfo.FileName = exePath; process.StartInfo.Arguments = exeArgs; if (isHidden) { process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.CreateNoWindow = true; } else { process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; process.StartInfo.CreateNoWindow = false; } process.Start(); int idx = 1; while (!process.HasExited) { idx++; process.WaitForExit(1000); if (idx == 50) { process.Kill(); isSucc = false; } } process.Close(); process.Dispose(); return isSucc; } } }
Best Regards,
Alex
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已编辑 Alex-KSGZ 2019年5月31日 9:17
- 已标记为答案 TheYangtzeTheYellowRiver 2019年6月1日 2:44