C# 根据注册表获取当前用户的常用目录整理
2017-03-15 10:06
495 查看
1.使用C#获取当前程序或解决方案的路径
2.使用C#获取当前登录用户的相关目录
3.也可以获取当前系统通用目录
4.获取Windows系统的目录,从注册表中获取。
一、当前用户的目录,HKEY_Current_User
View Code
更多:
要将程序集“xxx.dll”标记为系统必备组件,必须对其进行强签名
设置c#windows服务描述及允许服务与桌面交互的几种方法(转)
Windows服务简单实例
2.使用C#获取当前登录用户的相关目录
3.也可以获取当前系统通用目录
4.获取Windows系统的目录,从注册表中获取。
一、当前用户的目录,HKEY_Current_User
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 }
View Code
更多:
要将程序集“xxx.dll”标记为系统必备组件,必须对其进行强签名
设置c#windows服务描述及允许服务与桌面交互的几种方法(转)
Windows服务简单实例
相关文章推荐
- 根据当前登录域账号 获取AD用户姓名和所在OU目录
- 根据当前登录域账号 获取AD用户姓名和所在OU目录
- C# 6种获取程序当前所执行目录的方法
- C# 根据当前时间获取,本周,本月,本季度等时间段
- 用C#获取MOSS中当前登录用户的信息
- C# 根据当前时间获取,本周,本月,本季度等时间段
- C#、获取当前目录
- 如何在C#中获取“当前目录”
- 转:c# 根据当前时间获取,本周,本月,本季度,月初,月末,各个时间段
- c# 根据当前时间获取,本周,本月,本季度,月初,月末,各个时间段
- 如何在C#中获取“当前目录”
- C# 根据当前时间获取,本周,本月,本季度等时间段 .Net中Exception
- C#中获取程序当前路径的7种最常用的方法
- 如何在C#中获取“当前目录”
- java获取用户的当前工作目录
- c# 根据当前时间获取,本周,本月,本季度,月初,月末,各个时间段
- C#获取应当程序当前目录的方法
- C# 根据当前时间获取,本周,本月,本季度等时间段
- 如何在C#中获取“当前目录”
- C# 获取当前目录