zoukankan      html  css  js  c++  java
  • C#获取用户桌面等特殊系统路径

    不同的操作系统,桌面的路径不尽相同,而且随着用户安装位置的不同也不同。
    C#可以从Windows注册表读取得到用户的特殊文件夹(桌面、收藏夹等等)的位置。
    代码如下:

    using Microsoft.Win32;
    namespace JPGCompact
    {
        public partial class MainForm : Form
        {
            private void Test()
            {
                RegistryKey folders;
                folders = OpenRegistryPath(Registry.CurrentUser, @"\software\microsoft\windows\currentversion\explorer\shell folders");
                // Windows用户桌面路径
                string desktopPath = folders.GetValue("Desktop").ToString();
                // Windows用户字体目录路径
                string fontsPath = folders.GetValue("Fonts").ToString();
                // Windows用户网络邻居路径
                string nethoodPath = folders.GetValue("Nethood").ToString();
                // Windows用户我的文档路径
                string personalPath = folders.GetValue("Personal").ToString();
                // Windows用户开始菜单程序路径
                string programsPath = folders.GetValue("Programs").ToString();
                // Windows用户存放用户最近访问文档快捷方式的目录路径
                string recentPath = folders.GetValue("Recent").ToString();
                // Windows用户发送到目录路径
                string sendtoPath = folders.GetValue("Sendto").ToString();
                // Windows用户开始菜单目录路径
                string startmenuPath = folders.GetValue("Startmenu").ToString();
                // Windows用户开始菜单启动项目录路径
                string startupPath = folders.GetValue("Startup").ToString();
                // Windows用户收藏夹目录路径
                string favoritesPath = folders.GetValue("Favorites").ToString();
                // Windows用户网页历史目录路径
                string historyPath = folders.GetValue("History").ToString();
                // Windows用户Cookies目录路径
                string cookiesPath = folders.GetValue("Cookies").ToString();
                // Windows用户Cache目录路径
                string cachePath = folders.GetValue("Cache").ToString();
                // Windows用户应用程式数据目录路径
                string appdataPath = folders.GetValue("Appdata").ToString();
                // Windows用户打印目录路径
                string printhoodPath = folders.GetValue("Printhood").ToString();
            }
    
            private RegistryKey OpenRegistryPath(RegistryKey root, string s)
            {
                s = s.Remove(0, 1) + @"\";
                while (s.IndexOf(@"\") != -1)
                {
                    root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"\")));
                    s = s.Remove(0, s.IndexOf(@"\") + 1);
                }
                return root;
            }
        }
    }
  • 相关阅读:
    【免费赠书】前端程序员修炼之路:积土而为山,积水而为海
    开源网站流量统计系统Piwik源码分析——后台处理(二)
    开源网站流量统计系统Piwik源码分析——参数统计(一)
    数学思维修炼
    《代码大全》中的变量
    linux dmesg命令参数及用法详解(linux显示开机信息命令)
    dd命令使用详解
    Linux就这个范儿 第10章 生死与共的兄弟
    Linux就这个范儿 第9章 特种文件系统
    Bash中的shopt选项
  • 原文地址:https://www.cnblogs.com/myitm/p/2218701.html
Copyright © 2011-2022 走看看