I wanna write program that detects flash drives.
But there's a problem.
Code:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Removable)
{
}
}
It works well, but it detects cdrom too. How to prevent it?
I have no answer why your code is not working. But if you want to detect USB-devices, you could also try it with the WMI like this:
ManagementObjectCollection drives = new ManagementObjectSearcher(
"SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
).Get();
Add the System.Management assembly to your project to do it like this.
Related
How can I dismount a logical drive temporarily on my computer in c#? I am looking for a code that gives me similar results
Dismount/Hide Drives
//Do some work
Mount / Show Drives
I had tried this code to renaming the drive letters, but cannot understand how to use the Dismount method
ManagementObjectSearcher searchDrives = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Volume WHERE label = 'Data'");
foreach (ManagementObject Drive in searchDrives.Get())
{
Drive.Get();
Drive["DriveLetter"] = "M:";
Drive.Put();
}
This is the Method provided by WMI
https://learn.microsoft.com/en-us/previous-versions/windows/desktop/vdswmi/dismount-method-in-class-win32-volume
How should I use it, please?
I have been trying to find the amount of free storage space available in the fixed drive on a device in my UWP app. I have been using the following code to achieve this-
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.DriveType == DriveType.Fixed && d.IsReady)
{
double availableFreeSpaceInBytes = d.AvailableFreeSpace;
}
}
But whenever I run this, d.IsReady always returns false indicating that the device is not ready. I referred this- https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.isready?view=netframework-4.8. But haven't been able to understand.
Please help me with what am I doing wrong. Or is there any other way to achieve this?
If you only need to know the free space on the drive where your UWP app is installed (usually, the C: drive), you can use the following without adding any additional capabilities:
using Windows.Storage;
string freeSpaceKey = "System.FreeSpace";
var retrieveProperties = await ApplicationData.Current.LocalFolder.Properties.RetrievePropertiesAsync(new string[] { freeSpaceKey });
var freeSpaceRemaining = (ulong)retrieveProperties[freeSpaceKey];
Retrieving the amount of local storage on a device in UWP app
AvailableFreeSpace is not available in UWP system. For getting available free space, you need use StorageFolder System.FreeSpace property to achieve. Pleas note if you used GetFolderFromPathAsync metod, you need to allow broadFileSystemAccess capability before. Please refer this case link.
const String k_freeSpace = "System.FreeSpace";
const String k_totalSpace = "System.Capacity";
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
try
{
Debug.WriteLine("Drive: " + d.Name);
Debug.WriteLine("RootDir: " + d.RootDirectory.FullName);
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(d.RootDirectory.FullName);
var props = await folder.Properties.RetrievePropertiesAsync(new string[] { k_freeSpace, k_totalSpace });
Debug.WriteLine("FreeSpace: " + (UInt64)props[k_freeSpace]);
Debug.WriteLine("Capacity: " + (UInt64)props[k_totalSpace]);
}
catch (Exception ex)
{
Debug.WriteLine(String.Format("Couldn't get info for drive {0}. Does it have media in it?", d.Name));
}
}
I need to check disk name and whether the "enable write caching on the device" is enabled or not. so far I tried api method DriveInfo.GetDrives , But it doesn't have any property or methods that can return information I am looking for.
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" Drive type: {0}", d.DriveType);
}
https://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives(v=vs.110).aspx
I am wondering if I can close an explorer window which is communicating with my USB drive. I can get the removable disk and its drive letter by using
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (!drive.IsReady)
{
continue;
}
if (drive.DriveType == DriveType.Removable && isDirectoryEmpty(drive.Name) == true)
{
//do stuff
}
}
How do I do that ? any help would be appreciated.
You cannot use Process.GetProcessesByName("explorer") because there is always one explorer process in the returned array, and by killing it, you would kill the window task bar too.
You have to use a COM library as explained here:
https://stackoverflow.com/a/13464352/1280523
You can try like this:
foreach (Process p in Process.GetProcessesByName("explorer"))
{
if (p.MainWindowTitle.ToLower().Contains(#"yourSpecificWindow"))
{
p.Kill();
}
}
i have tried this code to get the usb devices in connected to the computer.
This is the code:
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Removable)
{
cmbUSB.Items.Add(drive.Name);
}
}
cmbusb is a combobox.. here i am getting this :
E:/
G:/
but not getting the device name, like :
E:/Insforia
something like this,
how can i get this? is it possible to get this? pls help
For getting the DeviceName of E:/ try this.
DriveInfo driveInfo = new DriveInfo("E");
if(driveInfo.IsReady)
{
string deviceName = driveInfo.VolumeLabel;
}
I believe you are looking for VolumeLabel, try:
The label length is determined by the operating system. For example,
NTFS allows a volume label to be up to 32 characters long. Note that
null is a valid VolumeLabel.
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Removable)
{
if (drive.IsReady)
cmbUSB.Items.Add(drive.Name + "-" + drive.VolumeLabel);
//^^^^^^^^^^^^^^^^
//here
}
}