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; }
댓글 입력