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服务描述及允许服务与桌面交互的几种方法(转)

Windows服务简单实例

C# 根据注册表获取当前用户的常用目录整理的更多相关文章

  1. C# 系统应用之通过注册表获取USB使用记录(一)

    该文章是“个人电脑历史记录清除软件”项目的系统应用系列文章.前面已经讲述了如何清除IE浏览器的历史记录.获取Windows最近访问文件记录.清除回收站等功能.现在我需要完成的是删除USB设备上的U盘. ...

  2. 获取客户端用户真实ip方法整理(jekyll迁移)

    layout: post title: 获取客户端用户真实ip方法整理 date: 2019-08-22 author: xiepl1997 tags: springboot 由请求获取客户端ip地址 ...

  3. C#根据字体名通过注册表获取该字体文件路径(win10)两种方法推荐第二种

    方法一: 直接先上源码: private System.Collections.Generic.SortedDictionary<string, string> ReadFontInfor ...

  4. Inno Setup中做补丁通过注册表获取原程序安装目录

    今天找VM补丁看到的,是个innosetup封装的,所以习惯性的喜欢去看人家的iss文件是怎么编写的. DefaultDirName={reg:HKLM\SOFTWARE\VMware%2c%20In ...

  5. 『练手』通过注册表 获取 VS 和 SQLServer 文件路径

    获取任意 VS 和 SQLServer 的 磁盘安装目录. 背景需求:如果磁盘电脑安装了 VS 或者 SQLServer 则 认定这台计算机 的使用者 是一名 软件研发人员,则让程序 以最高权限运行. ...

  6. 读取注册表获取Windows系统XP/7/8/10类型(使用wcscmp比较wchar[]内容)

    很多方案是采用GetVersion.GetVersionEx这两个API来查询操作系统的版本号来判断当前的操作系统是Windows系列中的哪个,在Win10没有出现前,这种方法是行的通的,但是Win1 ...

  7. C#注册表操作类(完整版) 整理完整

    /// <summary> /// 注册表基项静态域 /// /// 主要包括: /// 1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键 /// ...

  8. 利用C#访问注册表获取软件的安装路径

    文章地址:https://blog.csdn.net/yl2isoft/article/details/17332139

  9. Win查询注册表获取CPU与内存参数

    #include "stdafx.h" void dump_machine_info( HANDLE fh ) { CHAR Str[MAX_PATH*3]; CHAR MHzSt ...

随机推荐

  1. 【LOJ】#2551. 「JSOI2018」列队

    题解 老年选手一道裸的主席树都要看好久才看出来 首先熟练的把这个区间建成\(n\)个主席树 然后对于一个询问,我们相当于在主席树上二分一个mid,使得\(mid - K + 1\)正好和\([l,r] ...

  2. 【LOJ】#2079. 「JSOI2016」轻重路径

    题解 写数据结构的时候我代码就会变得非常非常长 一看别人1.5K 2.3K 我6.3K-- orzzzzz 我们很容易想到离线倒着插入,然而,有个小锅叫如果size相同保持原来的重儿子不变 我们需要写 ...

  3. 【struts2基础】配置详解

    一.struts2工作原理(网友总结,千遍一律) 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做Action ...

  4. linux设置最大打开文件数

    一.查看当前用户对进程打开文件最大数的限制 $ ulimit -a | grep open 二.系统对进程打开文件最大数是如何限制的 先来看man的一段解析: /proc/sys/fs/file-ma ...

  5. jenkins 整合maven,svn(配置钩子程序实现提交代码自动构建),tomcat实现热部署(windows+linux分别实现)

    springboot : https://blog.csdn.net/zjh_746140129/article/details/80904876 1 准备工作: (1)运行jenkins的tomca ...

  6. speech模块实现语音识别

    1.pip安装speech.pywin32 pip install speech pip install pywin32 2.例子 #!/usr/bin/python # coding:utf-8 f ...

  7. 「PKUWC2018」猎人杀

    「PKUWC2018」猎人杀 解题思路 首先有一个很妙的结论是问题可以转化为已经死掉的猎人继续算在概率里面,每一轮一直开枪直到射死一个之前没死的猎人为止. 证明,设所有猎人的概率之和为 \(W\) , ...

  8. 关于Android4.X的Alertdialog对话框

    最近在做Android4.0的开发,发现AlertDialog相比较以前有了较大变化,就是在触摸对话框边缘外部,对话框消失 于是研究其父类发现,可以设置这么一条属性,当然必须先AlertDialog. ...

  9. 关于STM32数据手册中的定时器信号

    首先,我们可以看到这个图大概有两个不分,一个部分是时钟源,另一个部分则是输入输出 时钟源计数,到CNT计数器,然后根据捕获比较寄存器进行记录或比较.记录或比较有不同的配置. 首先是TI信号TI1 TI ...

  10. Syntactic and Semantic Errors

    There are two kinds of errors that Basis can find. Syntax errors occur during the parsing of input c ...