2017-07-16 8 views
0

Ich versuche, alle Ordner und Dateien in meinem Benutzerordner aufzulisten, der "Thomas" ist, dann möchte ich alle Ordner in diesen Ordnern sowie Dateien und so weiter abrufen. Aber immer, wenn ich es laufen, wirft es diese Ausnahme:C# UnauthorizedAccessException zu Benutzerordner

System.UnauthorizedAccessException: Access to the path 'C:\Users\Thomas\AppData\Local\Application Data' is denied. 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.IO.FileSystemEnumerableIterator`1.AddSearchableDirsToStack(SearchData localSearchData) 
    at System.IO.FileSystemEnumerableIterator`1.MoveNext() 
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
    at System.IO.Directory.GetDirectories(String path, String searchPattern, SearchOption searchOption) 
    at GetFilesInFolders.Program.Main(String[] args) in c:\users\thomas\documents\visual studio 2017\Projects\GetFilesInFolders\GetFilesInFolders\Program.cs:line 23 

Ich finde das seltsam, da ich die volle Berechtigung haben diesen Ordner zugreifen, aber aus irgendeinem Grund sagt andere weisen.

des Codes Alles ist unter:

using System; 
using System.IO; 
using System.Collections.Generic; 

namespace GetFilesInFolders 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Enter a path: "); 
      string path = Console.ReadLine(); 
      int UnauthorizedAccessCount = 0;//counter for file i cant access 
      List<string> DirList = new List<string>(); 
      List<string> FileList = new List<string>(); 
      try 
      { 
       if (Directory.Exists(path)) 
       { 
        foreach (string dir in Directory.GetDirectories(path, "*", SearchOption.AllDirectories)) 
        { 
         DirList.Add(dir); 
        } 
        foreach (string file in Directory.GetFiles(path, "*", SearchOption.AllDirectories)) 
        { 
         FileList.Add(file); 
        } 
       } 
       else 
       { 
        Console.WriteLine("Directory does not exist!"); 
        Console.ReadKey(); 
        return; 
       } 
      } 
      catch (UnauthorizedAccessException ex) 
      { 
       Console.WriteLine("{0}", ex); 
      } 
      if(FileList.Count == 0) 
      { 
       Console.WriteLine("There were no files, or you didn't have proper permissions. (You didn't have permission to {0} files or folders", UnauthorizedAccessCount); 
       Console.ReadKey(); 
      } 
      if(DirList.Count == 0) 
      { 
       Console.WriteLine("There were no folders, or you didn't have proper permissions. (You didn't have permission to {0} files or folders", UnauthorizedAccessCount); 
       Console.ReadLine(); 
      } 
      else 
      { 
       Console.WriteLine("Here are all the folders:\n"); 
       foreach (string dir in DirList) 
       { 
        Console.WriteLine(dir); 
       } 
       Console.WriteLine("Here are all the files:\n"); 
       foreach (string file in FileList) 
       { 
        Console.WriteLine(file); 
       } 
       if (UnauthorizedAccessCount != 0) 
       { 
        Console.WriteLine("You had no permission to access {0} files.", UnauthorizedAccessCount); 
       } 
       else 
       { 

       } 
       Console.ReadLine(); 
      } 

     } 
    } 
} 

Antwort

1

Sie sagen, Sie die volle Berechtigung haben, aber eine Konsole Skript mit dem priviliges des Kontos nicht ausgeführt. Es läuft mit Standardbenutzer-Privilegien, und App Data ist ein eingeschränkter Ordner in Windows (wo normale Benutzer nicht herumstochern sollen).

Ändern Sie Ihre Konsolenanwendung so, dass sie als Administrator ausgeführt wird. Siehe diese Antwort auf, wie man das macht:

How do I force my .NET application to run as administrator?

Verwandte Themen