使用C#返回系統特殊路徑
來源:互聯網
上載者:User
using System;
using Microsoft.Win32;
namespace SystemPathsApp
{
/// <summary>
/// Summary description for SystemPaths.
/// </summary>
public class SystemPaths
{
public SystemPaths()
{
//
// TODO: Add constructor logic here
//
folders = OpenRegistryPath(Registry.CurrentUser,@"\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders");
}
private RegistryKey folders;
private RegistryKey OpenRegistryPath(RegistryKey root,string s)
{
s = s.Remove(0,1) + @"\";
while (s.IndexOf(@"\") != -1)
{
root = root.OpenSubKey(s.Substring(0,s.IndexOf(@"\")));
s = s.Remove(0,s.IndexOf(@"\") + 1);
}
return root;
}
/// <summary>
/// </summary>
/// <param name="path">
/// Desktop 案頭目錄
/// Fonts 字型目錄
/// NetHood 網路位置
/// Personal 我的文件目錄
/// Programs 開始菜單程式目錄
/// Recent 存放使用者最近訪問檔案捷徑的目錄
/// SendTo 發送到目錄
/// StartMenu 開始菜單目錄
/// Startup 開始菜單啟動項目錄
/// Favorites 收藏夾目錄
/// History 網頁曆史目錄
/// Cookies cookies目錄
/// Cache 緩衝目錄
/// AppData 應用程式資料目錄
/// PrintHood 列印目錄
/// </param>
/// <returns></returns>
public string Customize(string path)
{
return folders.GetValue(path).ToString();
}
public string Desktop()
{
return Customize("Desktop");
}
public string Cookies()
{
return Customize("Cookies");
}
public string MyDocuments()
{
return Customize("Personal");
}
public string History()
{
return Customize("History");
}
public string Favorites()
{
return Customize("Favorites");
}
}
}