簡體   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