StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Yuichi  
#1 Posted : Thursday, August 15, 2019 11:10:14 AM(UTC)
Yuichi

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 9/13/2018(UTC)
Posts: 54
Poland

Thanks: 18 times
Was thanked: 18 time(s) in 13 post(s)
Hi

I decided to share part of my code BigGrin

This allows you to find a specific disk based on the label on your computer regardless of what
letter it was placed on the system and return its letter.

More info here.


Code:
// Drive types:
// "CDRom" = The drive is an optical disc device, such as a CD or DVD-ROM
// "Fixed" = The drive is a fixed disk
// "Network" = The drive is a network drive
// "NoRootDirectory" = The drive does not have a root directory
// "Ram" = The drive is a RAM disk
// "Removable" = The drive is a removable storage device, such as a USB flash drive
// "Unknown" = The type of drive is unknown

var driverLetter = null;
var volumeLabel = null;
var labelName = "PATRIOT"; // name of drive you looking for

var drives = clr.System.IO.DriveInfo.GetDrives();
for (i = 0; i < drives.Length; i++) {
    var driveType = drives[i].DriveType.ToString();
    if (drives[i].IsReady) { // if the drive is not ready, "drives[i].VolumeLabel" will cause an error
        if (driveType == "Removable") { // pendrive
            var volumeLabel = drives[i].VolumeLabel;
            if (volumeLabel.includes(labelName)) {
                driverLetter = drives[i].Name;
                break;
            }
        }
    }
}

if (driverLetter) {
    sp.MessageBox("Drive letter: " + driverLetter + "\nLabel : " +  volumeLabel, "Drive found");
} else {
    sp.MessageBox("Drive not found.", "Drive not found");
}
thanks 1 user thanked Yuichi for this useful post.
soooulp on 9/1/2022(UTC)
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.