我有制作.rar文件的程序。
public static void RarFilesT(string rarPackagePath, Dictionary<int, string> accFiles)
{
string[] files = new string[accFiles.Count];
int i = 0;
foreach (var fList_item in accFiles)
{
files[i] = "\"" + fList_item.Value;
i++;
}
string fileList = string.Join("\" ", files);
fileList += "\"";
System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo();
string cmdArgs = string.Format("A {0} {1} -ep",
String.Format("\"{0}\"", rarPackagePath),
fileList);
sdp.ErrorDialog = true;
sdp.UseShellExecute = true;
sdp.Arguments = cmdArgs;
sdp.FileName = rarPath;//Winrar.exe path
sdp.CreateNoWindow = false;
sdp.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(sdp);
process.WaitForExit();
}
此生产者需要一个文件列表的字符串数组来生成rar文件。 任何人都可以告诉我如何制作包含子文件夹和文件的完整文件夹的rar。 抱歉1错误,我还需要从指定文件夹中选择扩展文件。
答案 0 :(得分:3)
/// <summary>
/// Package files. (Build Rar File)
/// </summary>
/// <param name="rarPackagePath">Rar File Path</param>
/// <param name="accFiles">List Of Files To be Package</param>
public static string RarFiles(string rarPackagePath,
Dictionary<int, string> accFiles)
{
string error = "";
try
{
string[] files = new string[accFiles.Count];
int i = 0;
foreach (var fList_item in accFiles)
{
files[i] = "\"" + fList_item.Value;
i++;
}
string fileList = string.Join("\" ", files);
fileList += "\"";
System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo();
string cmdArgs = string.Format("A {0} {1} -ep1 -r",
String.Format("\"{0}\"", rarPackagePath),
fileList);
sdp.ErrorDialog = false;
sdp.UseShellExecute = true;
sdp.Arguments = cmdArgs;
sdp.FileName = winrarPath;//Winrar.exe path
sdp.CreateNoWindow = false;
sdp.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(sdp);
process.WaitForExit();
error = "OK";
}
catch (Exception ex)
{
error = ex.Message;
}
return error;
}
为此你可以用完整的文件夹路径制作rar。 -r参数可以递归文件夹和文件。 感谢旁观者。 而且你也可以指定包装的延伸。
string rarPackage = "E:/Backup.rar";
Dictionary<int, string> accFiles = new Dictionary<int, string>();
accFiles.Add(1, "D://*.txt");
accFiles.Add(2, "D://*.html");
accFiles.Add(3, "D://*.jpg");
RarFiles(rarPackage, accFiles);
public static void UnrarFiles(string rarPackagePath, string dir)
{
System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo();
string cmdArgs = string.Format("X {0} * {1}",
String.Format("\"{0}\"", rarPackagePath),
String.Format("\"{0}\"", dir));
sdp.Arguments = cmdArgs;
sdp.ErrorDialog = true;
sdp.UseShellExecute = true;
sdp.CreateNoWindow = false;
sdp.FileName = rarPath;
sdp.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(sdp);
process.WaitForExit();
}
答案 1 :(得分:1)
-r参数可以递归文件夹和文件..
所以你将“-r”添加到
string cmdArgs = string.Format("A {0} {1} -ep -r",
String.Format("\"{0}\"", rarPackagePath),
fileList);
答案 2 :(得分:1)
Ashish,感谢您发布此内容;这对我很有帮助,因为我在最后一刻被告知我必须先将某些文件放到FTP上。
但我很好奇 - 为什么accFiles是字典,而不是字符串[]或集合? Microsoft建议不要在公共API中传递字典:http://msdn.microsoft.com/en-us/library/dn169389(v=vs.110).aspx
此外,使用StringBuilder将清理构建文件列表。我建议这个:
public static string RarFiles(string rarPackagePath,
Collection<string> accFiles)
{
string error = "";
try
{
StringBuilder fileListBuilder = new StringBuilder();
foreach (var fList_item in accFiles)
{
fileListBuilder.Append("\"" + fList_item + "\" ");
}
string fileList = fileListBuilder.ToString();
... (no change from this point on)
}
再次,谢谢 - 这对我来说真的很有帮助。我希望我的建议对你也有帮助。