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. linux修改文件读写执行权限命令chmod

    之前用chmod的时候都是简单的类似下面这样使用: $ file 也有时候可能会修改一个目录下所有子目录和文件: $ directory -R 也知道3个数字(例子中的755)分别代表赋予 “文件属主 ...

  2. Linux 获取目录中最后一个文件的名字

    find /application/docker_hub/logs/fof1private/amount_dev -type l | xargs basename

  3. 036 SQLContext和HiveContext

    1.SqlContext SQLContext依赖SparkContext 功能:支持SparkSQL操作(不依赖Hive) SQLContext在一个JVM中默认允许存在多个 只有SQLContex ...

  4. babel更新之后的 一些坑

    最近在使用babel-loader的时候,发生了一些错误,现在的babel-loader版本已经是8.0.0,更新到这个版本之后,如果还按照以前的安装依赖的方法: cnpm install --sav ...

  5. BZOJ.3998.[TJOI2015]弦论(后缀自动机)

    题目链接 \(Description\) 给定字符串S,求其第K小子串.(若T=0,不同位置的相同子串算1个:否则算作多个) \(Solution\) 建SAM,处理出对于每个节点,它和它的所有后继包 ...

  6. 20172308《Java软件结构与数据结构》第三周学习总结

    教材学习内容总结 第 5 章 队列 队列: 一种线性集合,其元素从一端加入,从另一端删除 元素处理:FIFO 与栈的比较 异:(1) 栈的处理过程只在栈的某一端进行:队列的处理过程在队列的两端进行.( ...

  7. JNI之String类型

    JNI使用的是改良的UTF-8格式的Strings. 以下文档来自官方: Modified UTF-8 Strings The JNI uses modified UTF-8 strings to r ...

  8. centos 7.2 安装mysql 修改 初始密码

    # /etc/init.d/mysqld stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &# my ...

  9. Delphi : Analyze PE file headers?

    Analyze PE file headers? { You'll need a OpenDialog to open a Exe-File and a Memo to show the file i ...

  10. One-wire Demo on the STM32F4 Discovery Board

    One-wire Demo on the STM32F4 Discovery Board Some of the devs at work were struggling to get their s ...