Reputation: 309
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";
Upvotes: 1
Views: 481
Reputation: 8699
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");
Upvotes: 4
Reputation: 103447
You can do this with simple string concatenation:
string path = rootDrive + @"Users\Perry Craft\Desktop\password.txt";
Upvotes: 2