Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

c# - Why am I getting an access denied error for the Documents and Settings folder?

I'm writing a program that gets all directories and sub-directories. I'm using the following code:

DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
    string[] directories = Directory.GetDirectories(drive.Name, "*", SearchOption.AllDirectories);
}

But I'm getting an exception stating "Access to the path 'C:Documents and Settings' is denied."

I'm using Windows 7 and I don't see a C:Documents and Settings folder in Explorer. I enabled "Show hidden files and folders" and even try to type in the path directly but it gives the following error: "C:Documents and Settings is not accessible. Access denied."

Why is Directory.GetDirectories() pulling a directory that doesn't seem to exist?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This directory is what is known as a junction point, which should be pointing to c:users.

From the MSDN documentation:

These junction points can be identified as follows:

They have the FILE_ATTRIBUTE_REPARSE_POINT, FILE_ATTRIBUTE_HIDDEN, and FILE_ATTRIBUTE_SYSTEM file attributes set.

They also have their access control lists (ACLs) set to deny read access to everyone.

Applications that call out a specific path can traverse these junction points if they have the required permissions. However, attempts to enumerate the contents of the junction points will result in failures.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...