C# 使用根目录字符串

C# 使用根目录字符串,c#,C#,因此,我使用以下代码查找当前windows“主驱动器” 我想做的是用rootDrive的字符串值替换“c:\”中的c,这样即使windows驱动器是J:\它也会使用该字符并能够保存到用户桌面。我是否需要以某种方式解析rootDrive字符串,或者我的想法是错误的,我应该做的就是重新编写它来表示 string rootDrive = Path.GetPathRoot(Environment.SystemDirectory); string path =@"rootDrive:\Users\Perr

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

我想做的是用rootDrive的字符串值替换“c:\”中的c,这样即使windows驱动器是J:\它也会使用该字符并能够保存到用户桌面。我是否需要以某种方式解析rootDrive字符串,或者我的想法是错误的,我应该做的就是重新编写它来表示

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

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

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

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

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

如果您需要桌面目录,请通过询问直接获取。无法保证用户的桌面位于
X:\Users\%username%\desktop
(其中X是系统卷);事实上,Windows和用户配置文件目录完全位于不同的卷上

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

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

如果您需要桌面目录,请通过询问直接获取。无法保证用户的桌面位于
X:\Users\%username%\desktop
(其中X是系统卷);事实上,Windows和用户配置文件目录完全位于不同的卷上

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

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