Home > Enterprise >  UnRAR DLL unable to be referenced in C#
UnRAR DLL unable to be referenced in C#

Time:04-08

I was trying to make a program to clean out some directories on my NAS and I noticed that a lot of folders contained nested rar and zip files and I have plenty of space to unpack them. The program should ask the user for a directory to be cleaned then unpack all rars then delete all of the rars. I'm trying to use UnRAR DLL and I cant even get the rars to unpack. I realize I'm having an issue where visual studio 2022 is refusing to recognize the Unrar DLL in the "using" command. Because of that I've been unable to unpack a single file. This is one my first useful programs so if im missing something basic I understand.

This is my initial attempt:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using UnRAR;



namespace Cleaning
{

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Directory To Be Cleaned");

            string rar_path = Console.ReadLine();

            string[] Rars = Directory.GetFiles(rar_path, "*.rar", SearchOption.AllDirectories);



            foreach (string rar in Rars)
            {

                string source = rar;
                string dest = "C:\\Users\\Kaleb\\OneDrive\\Desktop\Test Area";
                UnRAR unrar = new UnRAR();
                unrar.Password = "password_of_myarchive";
                unrar.Open(@source, UnRAR.OpenMode.Extract);
                while (unrar.ReadHeader())
                {
                    unrar.ExtractToDirectory(@dest);
                }
                unrar.Close();
            }
            

            
        }

       

        
    }
}

For reference I have added the UnRAR DLL to the project folder.

CodePudding user response:

SO I was able to get it working with the source code from the great people over at SharpCompress and utilizing their source I've got the following stable build.

using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Common;
using System;
using System.IO;
using System.Linq;
using System.Globalization;



namespace ConsoleApp3
{

    public class Program
    {
        static void Main(string[] args)
        {
            for (; ; )
            {
                Console.WriteLine("Enter E to extract all directories in file path");
                Console.WriteLine("Enter D to delete all Archives in file path");
                Console.WriteLine("REMEMBER TO ALWAYS EXTRACT BEFORE DELETING");
                string option = Console.ReadLine();


                if (option == "e" || option == "E")
                {

                   
                        Console.WriteLine("Enter Directory To Be Cleaned");
                    //as a warning this will extract all files from any rar in the slected driectory one at a time in order.
                    //if a rar is broken it will halt the program until the offendin rar is deleted best way to find is to see what has been extracted so far and go from there
                    //or one could also limit the directory in order to refine the number of rars to look for

                        string rar_path = Console.ReadLine();

                        string[] Rars = Directory.GetFiles(rar_path, "*.rar", SearchOption.AllDirectories);




                        foreach (string rar in Rars)
                        {
                            var DirectoryFinal = Path.GetDirectoryName(rar);
                            using (var archive = RarArchive.Open(@rar))
                            {
                                foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
                                {
                                    entry.WriteToDirectory(@DirectoryFinal, new ExtractionOptions()
                                    {
                                        ExtractFullPath = true,
                                        Overwrite = true
                                    });
                                }

                            };
                        }
                    
                   
                }


                else if (option == "d" || option == "D")
                {
                    Console.WriteLine("Enter Directory To Be Cleaned");
                    //be careful with this i would recomend extracting and then chekcing everything first

                    string rar_path = Console.ReadLine();
                    string[] TobeDeleted = Directory.GetFiles(rar_path, "*.r*", SearchOption.AllDirectories);
                    foreach (string rarstobedeleted in TobeDeleted)
                    {
                        File.Delete(rarstobedeleted);
                    }
                }
                else
                {
                    Console.WriteLine("Thats not an option try again");
                   
                }
                Console.WriteLine("Cleaning Complete.");

                ;
            }
        }
    



    }
}

This work effectively for rar files only for the time being but will effectively clean up any directories where someone may have downloaded a large amount of files stored in separated rars

  • Related