C#取各种目录
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2017-06-09 19:23:31
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
1、取程序当前运行目录路径
string dir=System.AppDomain.CurrentDomain.BaseDirectory;//获取应用程序的当前工作目录
2、取桌面路径
string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); MessageBox.Show("桌面的路径是"+dir);
3、
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; }