Error while accessing AvailableFreeSpace/TotalSize in DriveInfo in UWP

旧城冷巷雨未停 提交于 2019-12-13 02:51:25

问题


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.


回答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>


来源:https://stackoverflow.com/questions/52252751/error-while-accessing-availablefreespace-totalsize-in-driveinfo-in-uwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!