繁体   English   中英

使用根目录字符串

[英]using root directory string

因此,我使用以下代码来查找当前的窗口“主驱动器”

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"))

我想做的是用rootDrive的字符串值替换“ c:\\”中的c,以便即使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是系统卷)上; 它是,实际上,完全可以适用于Windows和用户配置文件目录位于不同的卷。

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

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

您可以通过简单的字符串连接来做到这一点:

string path = rootDrive + @"Users\Perry Craft\Desktop\password.txt";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM