在 UWP 的 DriveInfo 中访问 AvailableFreeSpace/TotalSize

我可以使用 DriveInfo.GetDrives() 方法列出本地磁盘。另外,我使用 Name 属性访问/获取驱动器名称。但我收到错误为“系统。未经授权的访问异常:'访问路径'X:\'被拒绝。” 在访问诸如 AvailableFreeSpace 之类的任何属性时。代码如下。


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

}

注意:我在 Package Tag Block 和 <rescap:Capability Name="broadFileSystemAccess"/> 中放置了以下行 xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"我项目的 Package.appxmanifest 文件中的功能标记块。


偶然的你
浏览 146回答 1
1回答

倚天杖

我可以使用以下代码为我的一些驱动器获取总的可用磁盘空间:&nbsp; &nbsp; &nbsp; &nbsp; const String k_freeSpace = "System.FreeSpace";&nbsp; &nbsp; &nbsp; &nbsp; const String k_totalSpace = "System.Capacity";&nbsp; &nbsp; &nbsp; &nbsp; DriveInfo[] allDrives = DriveInfo.GetDrives();&nbsp; &nbsp; &nbsp; &nbsp; foreach (DriveInfo d in allDrives)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.WriteLine("Drive: " + d.Name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.WriteLine("RootDir: " + d.RootDirectory.FullName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(d.RootDirectory.FullName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var props = await folder.Properties.RetrievePropertiesAsync(new string[] { k_freeSpace, k_totalSpace });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.WriteLine("FreeSpace: " + (UInt64)props[k_freeSpace]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.WriteLine("Capacity:&nbsp; " + (UInt64)props[k_totalSpace]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.WriteLine(String.Format("Couldn't get info for drive {0}.&nbsp; Does it have media in it?", d.Name));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }我的“最低版本”和“目标版本”都针对 Windows 10 版本 1803(10.0:Build 17134)。以下是我的 Package.appxmanifest 的一些摘录<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; IgnorableNamespaces="uap mp iot rescap">...<Capabilities>&nbsp; &nbsp; <Capability Name="internetClient" />&nbsp; &nbsp; <rescap:Capability Name="broadFileSystemAccess" />&nbsp; </Capabilities></Package>
打开App,查看更多内容
随时随地看视频慕课网APP