Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm using c# code for making .rar file of a folder.
C#
string zipFileToWrite, folderPath;
zipFileToWrite = @"D:\jack.rar";
folderPath = @"D:\TestFolder";
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();


This code makes an jack.rar file of TestFolder (All child folders and files) located in D:\.
Now i need to specify extensions of files for creating .rar file of that folder.
Can any one tell me how can i do that.
Posted
Comments
Sergey Alexandrovich Kryukov 15-Apr-13 3:14am    
What do you mean by this? Do you understand, that from the perspective of the file system, there is no such thing as "extension"? All file names and the parts of names are treated in the same way...
—SA

You can add an extra parameter while creating the zip files to mention the extension of files , you need to add to your zip file

C#
string zipFileToWrite, folderPath, pathExtensions;
       zipFileToWrite = @"D:\jack.rar";
       folderPath = @"D:\TestFolder";
       pathExtensions = "*.txt";
       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 + "\"" + " " + "\"" + pathExtensions + "\"" + " " + "\"" + folderPath + "\"";
       MyProcess.StartInfo.Arguments = "a -r " + "\"" + zipFileToWrite + "\"" + " " + "\"" + folderPath + "\"";
       MyProcess.Start();
       MyProcess.WaitForExit();



Please check the link for details
http://comptb.cects.com/2503-using-the-winrar-command-line-tools-in-windows[^]

Hope it Helps

[ Happy coding ]
 
Share this answer
 
v2
Hello Rakesh,

There are two ways you can achieve this

  1. Specify multiple extensions as *.asp *.gif *.css seperated by a space character as part of the command line
  2. Use an external file in which each extension is mentioned on a seperate line. This file name is then specified on the command line as @file_list.lst

Regards,
 
Share this answer
 
I were solved this problem by adding list of file as command argument.
My Code
C#
public static string RarFilesT(string rarPackagePath, Dictionary<int,> 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,> accFiles = new Dictionary<int,>();
    accFiles.Add(1, @"D:\New folder\New folder\*.txt");
    accFiles.Add(2, @"D:\New folder\*.html");
    RarFilesT(@"D:\test.rar",accFiles );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900