What API to retrieve PC Disk Drive properties? - c#

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

Related

Retrieving the amount of local storage on a device in UWP app

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));
}
}

Display General tab information of property window of local disks in my computer

I need to access the type of disk and display it in the C# code. In this image "
Local Disk Image" the type is local disk.
In this image "Cluster Disk Image" the type of disk is cluster disk.
I have used driveInfo class to get the drive details. but I'm not able to display whether the drive is cluster or local disk. d.DriveType returns whether it is a removable or fixed types. and not the required local disk or cluster type.
My code is added below:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" Drive type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
Is there any way to display the required info in C# code?
Thank you.

Detecting if disk is flash drive

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.

How to get list of USB in winform using c#?

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
}
}

Getting USB drive name attached to windows ce device

How to get name of usb drive attahced to my Windows ce device using c# or any or the native API.
You may be able to start with the C# DriveInfo class:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx
or this may help:
Get List of connected USB Devices
The folder name is localized and can change from device to device. The way I do it is to query it from the same registry location the driver used for getting the name:
using(var key = Registry.LocalMachine.OpenSubKey(
#"\System\StorageManager\Profiles\USBHDProfile"))
{
USBDiskFolderName = key.GetValue("Folder").ToString();
}

Categories

Resources