可以取得目前沒用到的磁碟代號。
public char getFreeLabel() { char letter = ' '; ArrayList driveLetters = new ArrayList(26); // Allocate space for alphabet for (int i = 65; i < 91; i++) // increment from ASCII values for A-Z { driveLetters.Add(Convert.ToChar(i)); // Add uppercase letters to possible drive letters } foreach (string drive in Directory.GetLogicalDrives()) { driveLetters.Remove(drive[0]); // removed used drive letters from possible drive letters } foreach (char drive in driveLetters) { // get first unused drive letters letter = drive; break; } return letter; }
會回傳A~Z,如果回傳的是空白,則代表己經沒有空的磁碟機代號了。
參考來源:
https://hardforum.com/threads/c-getting-free-drive-letters.1208456/
沒有留言:
張貼留言