简体   繁体   English

使用根目录字符串

[英]using root directory string

So, i am using the code as follows to find out the current windows "main drive" 因此,我使用以下代码来查找当前的窗口“主驱动器”

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

what I am trying to do is replace the c in "c:\\" with the string value of rootDrive so that even if the windows drive is J:\\ it would use that character and be able to save to the users desktop. 我想做的是用rootDrive的字符串值替换“ c:\\”中的c,以便即使Windows驱动器是J:\\,它也将使用该字符并将其保存到用户桌面。 Do I need to parse the rootDrive string somehow or am i wrong in my thinking that all I should have to do is re write it to say . 我是否需要以某种方式解析rootDrive字符串,或者我以为我应该做的就是重新编写它以表达意见,所以我错了。

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

If you need the desktop directory, interrogate Environment.GetFolderPath to get it directly. 如果需要桌面目录,请查询Environment.GetFolderPath以直接获取它。 There is no assurance that the user's desktop is located on X:\\Users\\%username%\\desktop (where X is the System volume); 无法保证用户的桌面位于X:\\Users\\%username%\\desktop (其中X是系统卷)上; it is, in fact, entirely possible for Windows and User Profile directories to be located on different volumes. 它是,实际上,完全可以适用于Windows和用户配置文件目录位于不同的卷。

For instance, to retrieve the path to password.txt on the desktop, use: 例如,要在桌面上检索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