• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# 根据注册表获取当前用户的常用目录整理

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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
    }
View Code

 

 

 

更多:

要将程序集“xxx.dll”标记为系统必备组件,必须对其进行强签名

设置c#windows服务描述及允许服务与桌面交互的几种方法(转)

Windows服务简单实例


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[转]C#堆和栈的区别发布时间:2022-07-13
下一篇:
c#格式化数字与日期发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap