在 UWP 中访问 DriveInfo 中的 AvailableFreeSpace/TotalSize 时出错 [英] Error while accessing AvailableFreeSpace/TotalSize in DriveInfo in UWP
问题描述
我可以使用 DriveInfo.GetDrives() 方法列出本地磁盘.此外,我使用 Name 属性访问/获取驱动器名称.但我收到错误消息System. UnauthorizedAccess Exception: 'Access to the path 'X:\' is denied".在访问任何属性时,如可用自由空间.代码如下.
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);
}
注意:我在包标签块中放置了以下几行 xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" 和 <rescap:Capability Name="broadFileSystemAccess"/> 在我的项目的 Package.appxmanifest 文件中的 Capabilities 标签块内.
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.
推荐答案
我能够使用此代码获得某些驱动器的总和可用磁盘空间:
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));
}
}
我的最低版本"和目标版本"都针对 Windows 10 版本 1803(10.0:Build 17134).
I am targeting Windows 10, version 1803 (10.0: Build 17134) for both my "Min version" and "Target version".
这是我的 Package.appxmanifest 的一些摘录
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>
这篇关于在 UWP 中访问 DriveInfo 中的 AvailableFreeSpace/TotalSize 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!