2017-02-08 43 views
0

我需要從C:\ProgramData\Microsoft\Windows\Start Menu\Programs及其子文件夾獲取文件。如何訪問開始菜單路徑?

我試圖讓他們像這樣:

string path = @"C:\ProgramData\Microsoft\Windows\Start Menu\"; 
string[] lnks = Directory.GetFiles(path, "*.lnk", SearchOption.AllDirectories);` 

但它給我一個錯誤:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Access denied to the path : 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs' 

那些是什麼,我試圖讓訪問該路徑;

<requestedExecutionLevel level="highestAvailable" uiAccess="false" /> to the app.manifest 

File.GetAccessControl(path);在代碼

啓動Visual Studio中的聯繫

他們沒有工作。那麼我怎麼才能從這個路徑獲取這些文件呢?

+0

你不能改變路徑,並把文件放在其他文件夾。最好的辦法是給予文件夾的安全權限。 –

回答

1

該文件夾的問題是存在ReparsePoint文件夾,其文化名稱已本地化(例如在我的計算機中,我有名爲「Programmi」(重新解析點)的文件夾和名爲「程序」的實際文件夾)

Directory.GetFiles似乎在嘗試讀取ReparsePoint文件夾時失敗,但你可以用代碼,避免這樣的

string path = @"C:\ProgramData\Microsoft\Windows\Start Menu\"; 
string[] dirs = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly); 
foreach (string s in dirs) 
{ 
    DirectoryInfo di = new DirectoryInfo(s); 
    if (!di.Attributes.HasFlag(FileAttributes.ReparsePoint)) 
    { 
     string[] lnks = Directory.GetFiles(s, "*.lnk", SearchOption.AllDirectories); 
    } 
} 
0

你可以試試這個

串ALLUSERS = Environment.GetEnvironmentVariable( 「ALLUSERSPROFILE」)+ 「\開始菜單\程序」;