I have below code to get individual drive's information but I want the whole HDD informationie.I.E. total size and used or available space
public List<DriveInformation> GetDriveInfo()
{
List<DriveInformation> info = new List<DriveInformation>();
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
DriveInformation temp = new DriveInformation();
temp.driveName = drive.Name;
temp.driveSize = drive.TotalSize.ToString();
temp.availableSize = drive.TotalFreeSpace.ToString();
info.Add(temp);
}
return info;
}
if I have an unallocated drive then drive info unable to show that space.
ex: I have 500GB HDD and there is 2(C & D) partitions 200 each then this function give only infromation about C and D. I also want the Unallocated space detail(I.E.another 100GB) whether it is used or unallocated.
Related
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));
}
}
Is there a way to show data of not initialised HDDs? I need an app to show if there are any not initialised disks. Trying that:
DriveInfo[] disks = DriveInfo.GetDrives();
foreach(DriveInfo d in disks)
{
Console.WriteLine(d.Name + " " + d.DriveFormat);
}
But it shows only already formatted partitions
I'd like to get full name of all connected microphones. I was googling to find out an answer but there was no answer that satisfies me.
Let me show some examples:
1.
ManagementObjectSearcher mo = new ManagementObjectSearcher("select * from Win32_SoundDevice");
foreach (ManagementObject soundDevice in mo.Get())
{
MessageBox.Show(soundDevice.GetPropertyValue("Caption").ToString());
// or
MessageBox.Show(soundDevice.GetPropertyValue("Description").ToString());
//or
MessageBox.Show(soundDevice.GetPropertyValue("Manufacturer").ToString());
//or
MessageBox.Show(soundDevice.GetPropertyValue("Name").ToString());
//or
MessageBox.Show(soundDevice.GetPropertyValue("ProductName").ToString());
}
All of these getters shows: "Device Audio USB" or "Device compatible with High Definition standard".
2.
WaveInCapabilities[] devices = GetAvailableDevices();
foreach(device in devices)
{
MessageBox.Show(device.ProductName);
}
The same answer: "Device Audio USB" or "Device compatible with High Definition standard".
I want to get the full name. I mean, something like: "Sennheiser microphone USB". Is it even possible? I found: Get the full name of a waveIn device but a link in it is broken and I don't see any dsound.lib for c# (to use DirectSoundCaptureEnumerate).
Am I missing anything? Or is there any other option?
#AnkurTripathi answer is correct but it returns a name that contains up to 32 characters. If anyone doesn't want this restriction then the best idea is to use an enumarator:
using NAudio.CoreAudioApi;
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
var devices = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
foreach (var device in devices)
MessageBox.Show(device.friendlyName);
It works perfect for me.
Try Naudio https://naudio.codeplex.com/
for (int n = 0; n < WaveIn.DeviceCount; n++)
{
this.recordingDevices.Add(WaveIn.GetCapabilities(n).ProductName);
comboBoxAudio.Items.Add(WaveIn.GetCapabilities(n).ProductName);
}
for get Full Name(FriendlyName):
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
foreach (MMDevice device in enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active))
{
this.recordingDevices.Add(device.FriendlyName);
}
public static string GetDriveType()
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo drive in allDrives)
{
return DriveInfo.DriveType;
if(DriveType.CDRom)
{
return DriveInfo.Name;
}
}
}
As you guys can probably see, there is quite a bit wrong with this code. Basically, I am trying to return the name of the drive to use later in the code, but only if the drive is a CDRom drive. How can I just check the name of the drive and return it so that I can interpret it later when I am programmatically opening the CD drive? Thanks!
You should return a list of strings in case there are more cd drives:
public static List<string> GetCDDrives()
{
var cdDrives = DriveInfo.GetDrives().Where(drive => drive.DriveType == DriveType.CDRom);
return cdDrives?.Select(drive => drive.Name).ToList();
}
I think that you need something like the following:
public static string GetCDRomName()
{
// Get All drives
var drives = DriveInfo.GetDrives();
var cdRomName = null;
// Iterate though all drives and if you find a CdRom get it's name
// and store it to cdRomName. Then stop iterating.
foreach (DriveInfo drive in allDrives)
{
if(drive.DriveType == DriveType.CDRom)
{
cdRomName = drive.Name;
break;
}
}
// If any CDRom found returns it's name. Otherwise null.
return cdRomName;
}
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
}
}