C# 根据注册表获取当前用户的常用目录整理
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(, ) + @"/";
while (str.IndexOf(@"/") != -)
{
root = root.OpenSubKey(str.Substring(, str.IndexOf(@"/")));
str = str.Remove(, str.IndexOf(@"/") + );
}
return root;
}
#endregion
}
更多:
要将程序集“xxx.dll”标记为系统必备组件,必须对其进行强签名
设置c#windows服务描述及允许服务与桌面交互的几种方法(转)
C# 根据注册表获取当前用户的常用目录整理的更多相关文章
- C# 系统应用之通过注册表获取USB使用记录(一)
该文章是“个人电脑历史记录清除软件”项目的系统应用系列文章.前面已经讲述了如何清除IE浏览器的历史记录.获取Windows最近访问文件记录.清除回收站等功能.现在我需要完成的是删除USB设备上的U盘. ...
- 获取客户端用户真实ip方法整理(jekyll迁移)
layout: post title: 获取客户端用户真实ip方法整理 date: 2019-08-22 author: xiepl1997 tags: springboot 由请求获取客户端ip地址 ...
- C#根据字体名通过注册表获取该字体文件路径(win10)两种方法推荐第二种
方法一: 直接先上源码: private System.Collections.Generic.SortedDictionary<string, string> ReadFontInfor ...
- Inno Setup中做补丁通过注册表获取原程序安装目录
今天找VM补丁看到的,是个innosetup封装的,所以习惯性的喜欢去看人家的iss文件是怎么编写的. DefaultDirName={reg:HKLM\SOFTWARE\VMware%2c%20In ...
- 『练手』通过注册表 获取 VS 和 SQLServer 文件路径
获取任意 VS 和 SQLServer 的 磁盘安装目录. 背景需求:如果磁盘电脑安装了 VS 或者 SQLServer 则 认定这台计算机 的使用者 是一名 软件研发人员,则让程序 以最高权限运行. ...
- 读取注册表获取Windows系统XP/7/8/10类型(使用wcscmp比较wchar[]内容)
很多方案是采用GetVersion.GetVersionEx这两个API来查询操作系统的版本号来判断当前的操作系统是Windows系列中的哪个,在Win10没有出现前,这种方法是行的通的,但是Win1 ...
- C#注册表操作类(完整版) 整理完整
/// <summary> /// 注册表基项静态域 /// /// 主要包括: /// 1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键 /// ...
- 利用C#访问注册表获取软件的安装路径
文章地址:https://blog.csdn.net/yl2isoft/article/details/17332139
- Win查询注册表获取CPU与内存参数
#include "stdafx.h" void dump_machine_info( HANDLE fh ) { CHAR Str[MAX_PATH*3]; CHAR MHzSt ...
随机推荐
- php实现https(tls/ssl)双向认证
php实现https(tls/ssl)双向认证 通常情况下,在部署https的时候,是基于ssl单向认证的,也就是说只要客户端认证服务器,而服务器不需要认证客户端. 但在一些安全性较高的场景,如银行, ...
- NAT虚拟网络配置
NAT虚拟网络配置(Linux能上网) 1.先设置虚拟机的虚拟网络,设置里面的子网ip和网关ip地址: 有两种方式:①setup命令(不选DHCP,因为它是动态分配IP地址的) ②vi /etc/s ...
- String、Date和Timestamp的互转
begin 2018年8月17日19:09:49 String.Date和Timestamp的互转 String和Date的互转 关于String和Date的互转,在java8后会有不同.因为java ...
- RPO漏洞学习
不能直接复制markdown上来真的是痛苦,图片还要手动上传. 算了,不贴了. 这是PDF版https://files.cnblogs.com/files/r00tuser/RPO%E6%BC%8F% ...
- Xamarin iOS教程之键盘的使用和设置
Xamarin iOS教程之键盘的使用和设置 Xamarin iOS使用键盘 在文本框和文本视图中可以看到,当用户在触摸这些视图后,就会弹出键盘.本节将主要讲解键盘的输入类型定义.显示键盘时改变输入视 ...
- Centos 首次运行MySQL
1:启动MySQL systemctl start mysqld.service 2:查看MySQL运行状态 systemctl status mysqld.service 3:查看默认密码 grep ...
- BZOJ2973 : 石头游戏
考虑到$lcm(1,2,3,4,5,6)=60$,所以操作序列每60秒一个循环. 将操作表示成转移矩阵的形式,预处理出前60秒的转移矩阵以及它们的乘积$B$. 那么t秒的转移矩阵为前$t\bmod 6 ...
- app分组
将项目中中的urls.py复制到app当中 清空项目名称文件夹下的urls.py文件中的内容,并写入一下内容 from django.conf.urls import url,include urlp ...
- java:线上问题排查常用手段
一.jmap找出占用内存较大的实例 先给个示例代码: import java.util.ArrayList; import java.util.List; import java.util.concu ...
- Redis主从同步分析(转)
一.Redis主从同步原理 1.1 Redis主从同步的过程 配置好slave服务器连接的master后,slave会建立和master的连接,然后发送sync命令.无论是第一次同步建立的连接还是连接 ...