所以,我正在使用如下代码找出当前窗口的“主驱动器”
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/