无法在 C# 中引用 UnRAR DLL

UnRAR DLL unable to be referenced in C#

我试图制作一个程序来清理我 NAS 上的一些目录,我注意到很多文件夹包含嵌套的 rar 和 zip 文件,我有足够的 space 来解压它们。该程序应询问用户要清理的目录,然后解压缩所有 ras,然后删除所有 ras。我正在尝试使用 UnRAR DLL,但我什至无法解压 rars。我意识到我遇到了 visual studio 2022 拒绝识别“使用”命令中的 Unrar DLL 的问题。因此,我一直无法解压单个文件。这是我的第一个有用的程序,所以如果我缺少一些我能理解的基本知识。

这是我的初步尝试:

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

            
        }

       

        
    }
}

作为参考,我已将 UnRAR DLL 添加到项目文件夹中。

所以我能够使用来自 SharpCompress 的优秀人员的源代码并利用他们的源代码获得以下稳定版本。

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.");

                ;
            }
        }
    



    }
}

这暂时只对 rar 文件有效,但会有效地清理任何目录,其中有人可能下载了大量存储在单独 ras 中的文件