簡體   English   中英

為什么我的Documents and Settings文件夾出現拒絕訪問錯誤?

[英]Why am I getting an access denied error for the Documents and Settings folder?

我正在編寫一個獲取所有目錄和子目錄的程序。 我正在使用以下代碼:

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

但我得到一個例外,說“訪問路徑'C:\\ Documents and Settings \\'被拒絕。”

我正在使用Windows 7,我在資源管理器中看不到C:\\ Documents and Settings \\文件夾。 我啟用了“顯示隱藏文件和文件夾”,甚至嘗試直接鍵入路徑,但它出現以下錯誤:“C:\\ Documents and Settings無法訪問。訪問被拒絕。”

為什么Directory.GetDirectories()拉出一個似乎不存在的目錄?

這個目錄就是所謂的連接點 ,它應該指向c:\\ users。

從MSDN文檔:

這些交接點可以識別如下:

它們具有FILE_ATTRIBUTE_REPARSE_POINT,FILE_ATTRIBUTE_HIDDEN和FILE_ATTRIBUTE_SYSTEM文件屬性集。

他們還將訪問控制列表(ACL)設置為拒絕對每個人的讀取訪問權限。

如果具有所需權限,則調用特定路徑的應用程序可以遍歷這些連接點。 但是,枚舉連接點內容的嘗試將導致失敗。

在尋找答案一段時間之后 - 我決定自己編寫代碼。

我在這里分享基本的想法,而不是完整的代碼 - 采取重要的部分,並使用它在您的代碼中實現它。

為我工作。

public void directoryCrawl(string startFolder)
    {

    try
    {
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

        /* here you can add "dir" to some kind of list of your choice. */

        foreach (System.IO.DirectoryInfo directory in dir.GetDirectories())
        {
            try
            {
                directoryCrawl(directory.FullName);
            }
            catch
            {
                Console.Writeline("Access denied to: \"" + directory.FullName + "\".");
            }
        }
    }
    catch
    {
        if (!String.IsNullOrEmpty(startFolder))
        {
            Console.Writeline("Access denied to: \"" + startFolder + "\".");
        }
        }
        return;
    }

我不知道如何解決,但我可以告訴你,WinXP使用了這條路徑。 期望能夠訪問該文件夾的舊程序不會與Win7兼容,因此Microsoft將其重定向到您的Users文件夾。

如果我執行開始 - >運行c:\\Documents and Settings我也會收到Access is Denied錯誤。 所以有些東西存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM