简体   繁体   中英

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. 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 .

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. There is no assurance that the user's desktop is located on X:\\Users\\%username%\\desktop (where X is the System volume); it is, in fact, entirely possible for Windows and User Profile directories to be located on different volumes.

For instance, to retrieve the path to password.txt on the desktop, use:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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