0

I am able to list the local disks using DriveInfo.GetDrives() method. also, I access/get the Drive name using Name property. But I get error as "System. UnauthorizedAccess Exception: 'Access to the path 'X:\' is denied." while accessing any properties like AvailableFreeSpace. Code below.

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
   Debug.WriteLine("Drive: " + d.Name); //This line executes w/o error!
   Debug.WriteLine("Drive: " + d.AvailableFreeSpace);
   Debug.WriteLine("Drive: " + d.TotalSize);
}

NB: I have placed the following lines xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" in Package Tag Block and < rescap:Capability Name="broadFileSystemAccess"/ > inside the Capabilities Tag Block, in Package.appxmanifest file in my Project.

Muhammad Touseef
  • 4,357
  • 4
  • 31
  • 75
Mg Bhadurudeen
  • 1,140
  • 1
  • 12
  • 24

1 Answers1

1

I am able to get total and available disk space for some of my drives using this code:

        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 am targeting Windows 10, version 1803 (10.0: Build 17134) for both my "Min version" and "Target version".

Here are a couple of excerpts from my Package.appxmanifest

<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" 
         xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" 
         xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" 
        xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10" 
        xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" 
        IgnorableNamespaces="uap mp iot rescap">

...

<Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="broadFileSystemAccess" />
  </Capabilities>
</Package>
Greg Thatcher
  • 1,303
  • 20
  • 29
  • Nice! You have got the properties through StorageFolder! I was struggling with drive properties instead of passing its path to StorageFolder. Good idea! – Mg Bhadurudeen Sep 17 '18 at 06:48