c# - 使用根目录字符串

标签 c#

所以,我正在使用如下代码找出当前窗口的“主驱动器”

string rootDrive = Path.GetPathRoot(Environment.SystemDirectory);   
string path =@"c:\Users\Perry Craft\Desktop\password.txt";
Console.WriteLine("Windows is installed on the " + rootDrive + " drive");
if (!File.Exists(@"C: \Users\%username%\Desktop"))

我想做的是将“c:\”中的 c 替换为 rootDrive 的字符串值,这样即使 Windows 驱动器是 J:\,它也会使用该字符并能够保存到用户桌面.我是否需要以某种方式解析 rootDrive 字符串,或者我认为我应该做的就是重新写它说 .

string rootDrive = Path.GetPathRoot(Environment.SystemDirectory);
string path =@"rootDrive:\Users\Perry Craft\Desktop\password.txt";

最佳答案

如果需要桌面目录,查询Environment.GetFolderPath直接得到它。无法保证用户的桌面位于 X:\Users\%username%\desktop(其中 X 是系统卷);事实上,它完全是possible Windows 和用户配置文件目录位于不同的卷上。

例如,要在桌面上检索到 password.txt 的路径,请使用:

string desktoppath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string path = Path.Combine(desktoppath, "password.txt");

关于c# - 使用根目录字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39337399/

相关文章:

c# - 在 C# 中设置模块化程序的最佳方式

c# - 如何使用自定义 WPF 控件上的依赖属性处理计算值

c# - 将 C# 字符串变量插入到 ASP.NET 代码后面的 Javascript 警报中

c# - 默认情况下,plinq 会对 Dictionary<string, string> 使用范围分区还是 block 分区?以及如何强制对方?

c# - NHibernate 映射索引超出范围

c# - 如何处置我的 Excel 应用程序

c# - linq select 和 group by

C# 局部类

C# 正确的程序结构

c# - EF - LINQ to Entities 不支持