如何用c#制作全文件夹的rar文件

本文关键字:文件夹 rar 文件 何用 | 更新日期: 2023-09-27 18:09:13

我有一个制作。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文件。有谁能告诉我怎样才能使一个完整的文件夹与子文件夹和文件。对不起,我犯了一个错误,我也需要从给定的文件夹中选择扩展文件。

如何用c#制作全文件夹的rar文件

更新函数

    /// <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); 

Un-Rar

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();
    }

-r参数可以递归文件夹和文件..

所以你给

加上"-r"
 string cmdArgs = string.Format("A {0} {1} -ep -r",
        String.Format("'"{0}'"", rarPackagePath),
        fileList);

Ashish,感谢你的发帖;这对我很有帮助,因为我在最后一刻被告知,我必须FTP的一些文件应该首先rred。

但我很好奇-为什么accFiles是一个字典,而不是字符串[]或集合?Microsoft建议不要在公共api中传递字典:http://msdn.microsoft.com/en-us/library/dn169389(v=vs.110).aspx

同样,使用StringBuilder将清理构建fileList。我的建议是:

    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)
    }
再一次,谢谢——这对我真的很有帮助。我希望我的建议对你也有帮助。