1.使用C#獲取當前程序或解決方案的路徑
2.使用C#獲取當前登錄用戶的相關目錄
3.也可以獲取當前系統通用目錄
4.獲取Windows系統的目錄,從注冊表中獲取。
一、當前用戶的目錄,HKEY_Current_User
二、系統通用目錄,當前機器,Hkey_Local_Machine
三、代碼實例

class LocalPathHelper { //windows當前用戶的注冊表鍵 private static RegistryKey folders; /// <summary> /// 全局指定當前獲取注冊表的根節點 /// 一般只有管理員身份運行,才能操作Registry.LocalMachine 對應的文件 /// </summary> public static RegistryKey RootKey { get; set; } static LocalPathHelper() { SetAsCurrentUser(); } /// <summary> /// 設置根節點為LocalMachine /// </summary> public static void SetAsLocalMachine() { RootKey = Registry.LocalMachine; folders = OpenRegistryKey(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders"); } /// <summary> /// 設置根節點為LocalMachine /// </summary> public static void SetAsCurrentUser() { RootKey = Registry.CurrentUser; folders = OpenRegistryKey(Registry.CurrentUser, @"/software/microsoft/windows/currentversion/explorer/shell folders"); } #region 當前用戶路徑 /// <summary> /// windows用戶字體目錄路徑 /// </summary> public static string FontsPath { get { return GetPath("Fonts"); } } /// <summary> /// windows用戶網絡鄰居路徑 /// </summary> public static string NetHoodPath { get { return GetPath("Nethood"); } } /// <summary> /// windows用戶我的文檔路徑 /// </summary> public static string PersonalPath { get { return GetPath("Personal"); } } /// <summary> /// windows用戶最近訪問文檔快捷方式目錄 /// </summary> public static string RecentPath { get { return GetPath("Recent"); } } /// <summary> /// windows用戶發送到目錄路徑 /// </summary> public static string SendToPath { get { return GetPath("Sendto"); } } /// <summary> /// windows用戶收藏夾目錄路徑 /// </summary> public static string FavoritesPath { get { return GetPath("Favorites"); } } /// <summary> /// windows用戶網頁歷史目錄路徑 /// </summary> public static string HistoryPath { get { return GetPath("History"); } } /// <summary> /// windows用戶cookies目錄路徑 /// </summary> public static string CookiePath { get { return GetPath("Cookies"); } } /// <summary> /// windows用戶Cache目錄路徑 /// </summary> public static string CachePath { get { return GetPath("Cache"); } } #endregion #region //系統路徑 /// <summary> /// widnows用戶桌面路徑 /// </summary> public static string DesktopPath { get { return GetPath("Desktop"); } } /// <summary> /// windows用戶開始菜單程序路徑 /// </summary> public static string ProgramsPath { get { return GetPath("Programs"); } } /// <summary> /// windows用戶開始菜單目錄路徑 /// </summary> public static string StartMenuPath { get { return GetPath("StartMenu"); } } /// <summary> /// windows用戶開始菜單啟動項目路徑 /// </summary> public static string StartupPath { get { return GetPath("Startup"); } } /// <summary> /// windows用戶應用程序數據目錄 /// </summary> public static string AppdataPath { get { return GetPath("Appdata"); } } /// <summary> /// 公共文檔 /// </summary> public static string Documents { get { return GetPath("Documents"); } } #endregion /// <summary> /// 當前應用程序的工作目錄,不是程序文件目錄 /// </summary> public static string CurrentProgramPath { get { return Directory.GetCurrentDirectory(); } } /// <summary> /// 當前應用程序解決方案路徑 /// </summary> public static string CurrentSolutionPath { get { string program = CurrentProgramPath; DirectoryInfo info = new DirectoryInfo(program); return info.Parent.Parent.FullName; } } #region //私有方法 /// <summary> /// 獲取鍵值對應的文件夾 /// </summary> /// <param name="key">鍵</param> /// <returns>值</returns> private static string GetPath(string key) { if (RootKey == Registry.LocalMachine) key = "Common " + key; string path = folders.GetValue(key).ToString(); if (!string.IsNullOrEmpty(path)) { if (Directory.Exists(path)) { return path; } } return "'" + key + "'對應的文件夾不存在"; } //打開,指定根節點和路徑的注冊表項 private static RegistryKey OpenRegistryKey(RegistryKey root, string str) { str = str.Remove(0, 1) + @"/"; while (str.IndexOf(@"/") != -1) { root = root.OpenSubKey(str.Substring(0, str.IndexOf(@"/"))); str = str.Remove(0, str.IndexOf(@"/") + 1); } return root; } #endregion }
更多:
要將程序集“xxx.dll”標記為系統必備組件,必須對其進行強簽名