Question

Hi everyone those who are coding.I need to find solution for my application.Actually i did but there a little to go.Here is the code that compress file into rar formatted file that i mentioned.

  private const string RarPath = @"C:\Program Files\WinRAR\WinRAR.exe";
    List<string> files = new List<string>();




    public void RarFilesAdd(string rarPackagePath, List<string> files)
    {
        for (int i = 0; i < files.Count; i++) files[i] = string.Format("\"{0}\"", files[i]);
            string dosyaListe = string.Join(" ", files.ToArray());
            string arguman = string.Format("A {0} {1}", String.Format("\"{0}\"", rarPackagePath), dosyaListe);
            Process.Start(String.Format("\"{0}\"", RarPath), arguman);

    }
     static void Main(string[] args)
    {
        // files.AddRange(System.IO.Directory.GetFiles(@""));
        //RarFilesAdd(@"C:\my\test.rar", files);


        Program nesne = new Program();
        //
        nesne.files.Add(@"C:\Users\Hüseyin\Desktop\AA\a.txt");
        nesne.files.Add(@"C:\Users\Hüseyin\Desktop\AA\a1.txt");
        nesne.RarFilesAdd(@"C:\Users\Hüseyin\Desktop\deneme.rar",nesne.files);
       // nesne.files.AddRange(System.IO.Directory.GetFiles(@"C:\Users\Hüseyin\Desktop\deneme.rar"));

    }

This code creates rar file with full path like C:\Users\Hüseyin\Desktop\AA\a1.txt in deneme.rar. But i want that it creates as rar file without any folder in it. I guess i didn't explain in clearly.Now when i clicked deneme.rar it opens directory as Users\Hüseyin\AA . But i want files that situated in AA, keep in deneme.rar directly without any directory or files.I wanted to show screen that i want but i didn't do becaue of rep.

İf is there anyone who will help me it would be greatfull... Good days everyone

Was it helpful?

Solution

The command line you need is:

"C:\Program Files\WinRAR\WinRAR.exe" a -ep "C:\Users\Hüseyin\Desktop\deneme.rar" "C:\Users\Hüseyin\Desktop\AA\a.txt" "C:\Users\Hüseyin\Desktop\AA\a1.txt"

In your C# code I miss command a for add files to archive, but your code works nevertheless as this command is default on running WinRAR with no command specified.

And important is to use switch -ep which removes the paths from the file names and stores therefore in the RAR archive only the file names.

See text file Rar.txt in program files folder of WinRAR for details on available commands and switches.

Other command lines which could also work:

"C:\Program Files\WinRAR\WinRAR.exe" a -ep "C:\Users\Hüseyin\Desktop\deneme.rar" "C:\Users\Hüseyin\Desktop\AA\"

Adds all files in directory C:\Users\Hüseyin\Desktop\AA\ to the archive.

"C:\Program Files\WinRAR\WinRAR.exe" a -ep "C:\Users\Hüseyin\Desktop\deneme.rar" "C:\Users\Hüseyin\Desktop\AA\*.txt"

Adds only all files with file extension TXT in directory C:\Users\Hüseyin\Desktop\AA\ to the archive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top