[英]C# how to find the flashdrive letter and use it in a directory/string
我想找到閃存驅動器的盤符並創建一個文件,並將其放入找到的閃存驅動器中。
例如:
System.IO.File.Create(@"{Flash drive letter}\lol.txt");
您應該調用DriveInfo.GetDrives()返回一個DriveInfo對象的數組,然后對於每個驅動器,您都應注意DriveType ==可移動的驅動器。 一個初始代碼可能是:
var removableDrives = DriveInfo.GetDrives().Where(di => di.DriveType == DriveType.Removable);
foreach (var removableDrive in removableDrives){
var removableDrivePath = removableDrive.Name;
System.IO.File.Create(string.Format(@"{0}lol.txt", removableDrivePath));
}
請注意,實際上並非所有可移動驅動器都是閃存驅動器,並且計算機中可能有多個可移動驅動器。
另外,請注意,如果文件(在您的情況下為lol.txt)已經存在,則將其覆蓋,如果這就是您想要的,就可以了,如果不是您想要的,則應檢查此鏈接以使用其他文件模式創建文件並寫入滿足自己。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.