简体   繁体   English

C#UnauthorizedAccessException到用户文件夹

[英]C# UnauthorizedAccessException to User Folder

I'm trying to list all folders and files in my User folder which is "Thomas", then I want to get all of the folders in those folders as well as files and so on. 我试图列出我的用户文件夹“ Thomas”中的所有文件夹和文件,然后我想获取这些文件夹中的所有文件夹以及文件等。 But whenever I run it, it throws this exception: 但是,每当我运行它时,它都会引发以下异常:

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

I find this strange since I have FULL permission to access this folder, but for some reason it says other-wise. 我发现这很奇怪,因为我拥有访问此文件夹的完全许可权,但是由于某种原因,它表示相反。

All of the code is below: 所有代码如下:

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

        }
    }
}

You say you have full permission, but a console script does not run with the priviliges of the account. 您说您拥有完全许可权,但是控制台脚本不会以该帐户的特权运行。 It runs with default user priviliges, and App Data is a restricted folder in Windows (where normal users are not supposed to poke around). 它以默认用户权限运行,并且App Data是Windows中的受限文件夹(一般用户不应在其中浏览)。

Change your console application to actually run as administrator. 将您的控制台应用程序更改为以管理员身份实际运行。 See this answer on how to do that: 请参阅以下答案以了解如何执行此操作:

How do I force my .NET application to run as administrator? 如何强制.NET应用程序以管理员身份运行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM