C#获得特殊路径

本文介绍了一种使用C#和Windows注册表API来获取Windows系统中特定用户文件夹路径的方法,包括桌面、文档等常见位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using Microsoft.Win32;

02.namespace JPGCompact
03.{
04.    public partial class MainForm : Form
05.    {
06.        private void Test()
07.        {
08.            RegistryKey folders;
09.            folders = OpenRegistryPath(Registry.CurrentUser, @"/software/microsoft/windows/currentversion/explorer/shell folders");
10.            // Windows用户桌面路径
11.            string desktopPath = folders.GetValue("Desktop").ToString();
12.            // Windows用户字体目录路径
13.            string fontsPath = folders.GetValue("Fonts").ToString();
14.            // Windows用户网络邻居路径
15.            string nethoodPath = folders.GetValue("Nethood").ToString();
16.            // Windows用户我的文档路径
17.            string personalPath = folders.GetValue("Personal").ToString();
18.            // Windows用户开始菜单程序路径
19.            string programsPath = folders.GetValue("Programs").ToString();
20.            // Windows用户存放用户最近访问文档快捷方式的目录路径
21.            string recentPath = folders.GetValue("Recent").ToString();
22.            // Windows用户发送到目录路径
23.            string sendtoPath = folders.GetValue("Sendto").ToString();
24.            // Windows用户开始菜单目录路径
25.            string startmenuPath = folders.GetValue("Startmenu").ToString();
26.            // Windows用户开始菜单启动项目录路径
27.            string startupPath = folders.GetValue("Startup").ToString();
28.            // Windows用户收藏夹目录路径
29.            string favoritesPath = folders.GetValue("Favorites").ToString();
30.            // Windows用户网页历史目录路径
31.            string historyPath = folders.GetValue("History").ToString();
32.            // Windows用户Cookies目录路径
33.            string cookiesPath = folders.GetValue("Cookies").ToString();
34.            // Windows用户Cache目录路径
35.            string cachePath = folders.GetValue("Cache").ToString();
36.            // Windows用户应用程式数据目录路径
37.            string appdataPath = folders.GetValue("Appdata").ToString();
38.            // Windows用户打印目录路径
39.            string printhoodPath = folders.GetValue("Printhood").ToString();
40.        }
41.  
42.        private RegistryKey OpenRegistryPath(RegistryKey root, string s)
43.        {
44.            s = s.Remove(0, 1) + @"/";
45.            while (s.IndexOf(@"/") != -1)
46.            {
47.                root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/")));
48.                s = s.Remove(0, s.IndexOf(@"/") + 1);
49.            }
50.            return root;
51.        }
52.    }
53.}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值