如何指定多个文件扩展名来制作文件夹的rar?

时间:2013-04-15 06:19:31

标签: c# windows-applications winrar

我正在使用c#代码制作文件夹的.rar文件。

代码

string zipFileToWrite, folderPath;
zipFileToWrite = @"D:\jack.zip";
folderPath = @"D:\New folder";
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
MyProcess.StartInfo.WorkingDirectory = @"D:\NetworkPathChecking\FileBackup\FileBackup\bin\Debug\App_Files\";
MyProcess.StartInfo.FileName = "winrar.exe";
MyProcess.StartInfo.Arguments = "a -r " + "\"" + zipFileToWrite + "\"" + " " + "\"" + folderPath + "\"";
MyProcess.Start();
MyProcess.WaitForExit();

现在我需要指定multipule扩展名作为文件的过滤器来制作.rar来自文件夹。 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

使用带有* .FileExtension的文件列表解决了问题。

程序

public static string RarFilesT(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 = 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();
       error = "OK";
   }
   catch (Exception ex)
   {
       error = ex.Message;
   }
   return error;
}

通话程序

private void btnSave_Click(object sender, EventArgs e)
{
    Dictionary<int, string> accFiles = new Dictionary<int, string>();
    accFiles.Add(1, @"D:\New folder\New folder\*.txt");
    accFiles.Add(2, @"D:\New folder\*.html");
    RarFilesT(@"D:\test.rar",accFiles );
}

现在这个程序运行正常。它会对选定扩展名的文件夹进行完全备份。