c#有關獲取系統路徑

一、

系統路徑可以通過
Enviroment.SystemDirectory獲得
中間的參數爲Environment.SpecialFolder.DesktopDirectory,這樣獲得桌面目錄

返回“我的文檔”路徑字符串

Environment.GetFolderPath(Environment.SpecialFolder.Personal)
二、

不同的操作系統,桌面的路徑不盡相同,而且隨着用戶安裝位置的不同也不同。
C#可以從Windows註冊表讀取得到用戶的特殊文件夾(桌面、收藏夾等等)的位置。
代碼如下:

using Microsoft.Win32;
namespace JPGCompact
{
    public partial class MainForm : Form
    {
        private void Test()
        {
            RegistryKey folders;
            folders = OpenRegistryPath(Registry.CurrentUser, @"\software\microsoft\windows\currentversion\explorer\shell folders");
            // Windows用戶桌面路徑
            string desktopPath = folders.GetValue("Desktop").ToString();
            // Windows用戶字體目錄路徑
            string fontsPath = folders.GetValue("Fonts").ToString();
            // Windows用戶網絡鄰居路徑
            string nethoodPath = folders.GetValue("Nethood").ToString();
            // Windows用戶我的文檔路徑
            string personalPath = folders.GetValue("Personal").ToString();
            // Windows用戶開始菜單程序路徑
            string programsPath = folders.GetValue("Programs").ToString();
            // Windows用戶存放用戶最近訪問文檔快捷方式的目錄路徑
            string recentPath = folders.GetValue("Recent").ToString();
            // Windows用戶發送到目錄路徑
            string sendtoPath = folders.GetValue("Sendto").ToString();
            // Windows用戶開始菜單目錄路徑
            string startmenuPath = folders.GetValue("Startmenu").ToString();
            // Windows用戶開始菜單啓動項目錄路徑
            string startupPath = folders.GetValue("Startup").ToString();
            // Windows用戶收藏夾目錄路徑
            string favoritesPath = folders.GetValue("Favorites").ToString();
            // Windows用戶網頁歷史目錄路徑
            string historyPath = folders.GetValue("History").ToString();
            // Windows用戶Cookies目錄路徑
            string cookiesPath = folders.GetValue("Cookies").ToString();
            // Windows用戶Cache目錄路徑
            string cachePath = folders.GetValue("Cache").ToString();
            // Windows用戶應用程式數據目錄路徑
            string appdataPath = folders.GetValue("Appdata").ToString();
            // Windows用戶打印目錄路徑
            string printhoodPath = folders.GetValue("Printhood").ToString();
        }

        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;
        }
    }
}
http://apps.hi.baidu.com/share/detail/37823127

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章