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. Codeforces 336D Dima and Trap Graph 并查集

    Dima and Trap Graph 枚举区间的左端点, 然后那些左端点比枚举的左端点小的都按右端点排序然后并查集去check #include<bits/stdc++.h> #defi ...

  2. 模拟app端上传图片

    使用插件模块管理模式: jsp页面: <sys:fileUpload fieldName="picList" contentId="true" value ...

  3. 001.SMB简介

    一 简介 samba基于NetBIOS协议开发,能和windows通信,但只能在局域网通信. 二 Samba主要应用 文件共享 打印服务器 Samba登录时身份验证 进行windows主机名解析 三 ...

  4. kafka配置监控和消费者测试

    概念 运维 配置 监控 生产者与消费者 流处理 分区partition 一定条件下,分区数越多,吞吐量越高.分区也是保证消息被顺序消费的基础,kafka只能保证一个分区内消息的有序性 副本 每个分区有 ...

  5. Spring中数据绑定的两种方式(BeanWrapperImpl或者DataBinder)

    import org.apache.shiro.authc.AuthenticationToken; import org.springframework.beans.*; import org.sp ...

  6. poj-2421-最小生成树刷题

    title: poj-2421-最小生成树刷题 date: 2018-11-20 20:30:29 tags: acm 刷题 categories: ACM-最小生成树 概述 做了几道最小生成树的题, ...

  7. Android-Kotlin在Fragment获取View

    Android-Kotlin在Fragment获取View Overview 在使用Fragment的时候,使用了ButterKnife 来获取View但是一直出错,后来就直接使用Kotlin的导入布 ...

  8. Loj 10211 sumdiv

    题目描述 求 A^B 的所有约数之和 mod 9901. 首先,我们要求出A的约数之和. 就是把A分解质因数,成为:a1^k1*a2^k2*a3^k2.... 然后约数和就是(a1^0+a1^1+a1 ...

  9. 堆排序的C++代码实现

    堆排序C++实现 堆排序的具体思路可以查看<算法导论>这本书,一下只提供笔者的C++实现代码,并且将笔者在编写程序的过程当中所遇到的一些细节问题拿出来作一番解释,希望能够对对堆排序有一个透 ...

  10. Spring_Spring@Transactional

    Spring事务的传播行为 在service类前加上@Transactional,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务. Spring默认情况下会对运行期例 ...