C# C드라이브 경로의 모든 파일검색

조회수 2568회

C드라이브에 있는 모든 파일 리스트를 뽑아내고 싶은데

'경로에 대한 액세스가 거부되었습니다' 라고 자꾸 오류가 나네요

관리자 권한으로 실행하거나 검색을 통해 권한을 올리는 방법도 써보긴 했는데 제대로 안된건지

동작이 되지가 않네요 혹시 아시는분 있으시면 도와주세요.

<< 소스코드 >>

string[] files = Directory.GetFiles("C:\\",
                       "*.*",
                       SearchOption.AllDirectories);

            // Display all the files.
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }

1 답변

  • 관리자권한으로 코드를 실행해야할 것 같네요. 다음 코드를 추가해보시는게 어떨까요?

    관련 글

    using System.Security.Principal;
    public bool IsUserAdministrator()
    {
        bool isAdmin;
        try
        {
            WindowsIdentity user = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(user);
            isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        catch (UnauthorizedAccessException ex)
        {
            isAdmin = false;
        }
        catch (Exception ex)
        {
            isAdmin = false;
        }
        return isAdmin;
    }
    
    • 답변 감사합니다 반환값을 어떻게 사용해야 되는지 알려주실수 있으시나요? 알 수 없는 사용자 2018.6.1 13:58

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)