C# WinRAR 自解压问题

发布时间:2024-05-05 08:46 发布:上海旅游网

问题描述:

这是我在C# Winfrom里 写的一个关于制作“自解压格式”压缩包的方法:
private bool WinRAR()
{
string strtxtPath = "C:\\freezip\\free.txt";
string strzipPath = "C:\\freezip\\free.zip";
System.Diagnostics.Process Process1 =
new System.Diagnostics.Process();
Process1.StartInfo.FileName = "Winrar.exe";
Process1.StartInfo.CreateNoWindow = true;

strzipPath = "C:\\freezip\\free";//默认压缩方式为rar
Process1.StartInfo.Arguments = " a -s -sfx Setup=frees.exe "+ strzipPath + " " + strtxtPath ;
Process1.Start();
if (Process1.HasExited)
{
int iExitCode = Process1.ExitCode;
if (iExitCode == 0)
{
return false;
}
else
{
return false;
}
}
}

.... a -s -sfx 命令行已经可以实现“自解压格式”压缩包
但是我想在解压这个压缩包后会自动运行“frees.exe”程序
“Setup=<程序>”这个是 命令行
但是我写的“" a -s -sfx Setup=frees.exe "+ strzipPath + " " + strtxtPath ;”
却是错的...请高手帮忙解决下。
50分不多...重在技术交流。谢谢了先!!!
刷分者勿回~~ 刷分者勿回~~

问题解答:

/// <summary>
/// 调用winRAR
/// </summary>
/// <param name="name">压缩包名</param>
/// <param name="dirPath">打包源路径</param>
/// <param name="zipPath">压缩包存放位置</param>
/// <param name="smarkPath">备注文件 主意路径存在的空格</param>>
/// <returns></returns>
public string winRAR(string dirPath, string zipPath, string smarkPath, string defaultValue)
{
try
{
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
string WinRarPath = key.GetValue("").ToString();
if (!WinRarPath.Equals(""))
{
System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
Process1.StartInfo.FileName = "Winrar.exe";
Process1.StartInfo.CreateNoWindow = false;
string SFXPath = key.GetValue("Path").ToString() + "\\Wtwsm.SFX";
if (File.Exists(SFXPath))
{
Process1.StartInfo.Arguments = "a -r -ep1 " + zipPath + " -s -ibck -z" + smarkPath + " -sfx " + SFXPath + " " + dirPath.Trim();
}
else
{
Process1.StartInfo.Arguments = "a -r -ep1 " + "\"" + zipPath + "\"" + " -s -ibck -z" + smarkPath + " -sfx " + "\"" + dirPath.Trim() + "\"";
}
Process1.Start();
if (Process1.HasExited)
{
int iExitCode = Process1.ExitCode;
if (iExitCode == 0)
{
return defaultValue;
}
else
return "Error";
}
else
return "Error";
}
else
return "Error";
}
catch (Exception ex)
{
return ex.Message;
}
}

WinRAR();
结束了以后再去调用解压后exe路径去启动啊!

WinRAR();
这个说明已经解压结束了啊。exe已经解压出来了。然后知是拿到这个exe的路径,路径应该能获得把,要不你怎么解压,获得exe 路径再去启动这个exe啊。干嘛一定要解压后自动运行了。完全自己程序可以控制解压结束就去调用exe的

第一:问题没表述清楚

可以等待解压结束
Process1.WaitForExit();
int iExitCode = Process1.ExitCode;
if (iExitCode == 0)
{
return true;
}
else
{
return false;
}

热点新闻