c# - 如何显示本地文件夹uwp中的图像

标签 c# uwp

我正在尝试在 UWP 应用程序中自动可视化(无需用户交互)保存在 C:\Myfolder\image.jpg 中的图像。我知道这是一个热门话题,UWP 应用程序只能访问文件系统的某些“部分”,但我也了解到有不同的解决方案可以解决这个问题,例如 this

试试这个: mainpage.cs

BitmapImage bitmap = new BitmapImage();
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"C:\MyFolder");
StorageFile file = await folder.GetFileAsync("image.jpg");
using (var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
     bitmap.SetSource(stream);
     image0.Source = bitmap;
}

还在 appxmanifest 中添加以下行:

<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="App">
<Extensions>
    <uap:Extension Category="windows.fileTypeAssociation">
      <uap:FileTypeAssociation Name="jpegfiles">
        <uap:SupportedFileTypes>
          <uap:FileType>.jpg</uap:FileType>
        </uap:SupportedFileTypes>
      </uap:FileTypeAssociation>
    </uap:Extension>
  </Extensions>
  </Application>
 </Applications>
 <Capabilities>
   <Capability Name="internetClient" />
   <Capability Name="privateNetworkClientServer" />
   <Capability Name="internetClientServer" />
   <uap:Capability Name="enterpriseAuthentication" />
   <uap4:Capability Name="userDataTasks" />
   <DeviceCapability Name="microphone" />
 </Capabilities>

不幸的是,我总是遇到这样的错误:System.UnauthorizedAccessException: 'Access is denied.

我还尝试在 Image 对象中写入图像的直接路径,例如:

 image0.Source = new BitmapImage(new Uri(@"C:\Myfolder\image.jpg"));

在这种情况下,应用启动但不显示任何内容。

也许我错过了什么或者我不明白如何设置文件访问权限。

最佳答案

现在在 UWP 中实际上有办法访问磁盘上任意位置的本地文件。您可以声明受限的 broadFileSystemAccess 功能(请参阅 docs ),这将允许您使用 StorageFile API 访问磁盘上的任何路径(请注意,您仍然可以不要使用经典的 System.IO API 访问这些路径)。 如果启用 broadFileSystemAccess,您的代码将按原样开始工作。

但是,要声明此功能,您的应用需要有一个充分的理由,因为它是对用户设备的潜在安全威胁。 Docs状态:

This is a restricted capability. Access is configurable in Settings > Privacy > File system. Because users can grant or deny the permission any time in Settings, you should ensure that your app is resilient to those changes. If you find that your app does not have access, you may choose to prompt the user to change the setting by providing a link to the Windows 10 file system access and privacy article. Note that the user must close the app, toggle the setting, and restart the app. If they toggle the setting while the app is running, the platform will suspend your app so that you can save the state, then forcibly terminate the app in order to apply the new setting. In the April 2018 update, the default for the permission is On. In the October 2018 update, the default is Off.

If you submit an app to the Store that declares this capability, you will need to supply additional descriptions of why your app needs this capability, and how it intends to use it. This capability works for APIs in the Windows.Storage namespace. See the Example section at the end of this article for an example of how to enable this capability in your app.

不过,有些位置,例如 ApplicationData.Current.LocalFolderApplicationData.Current.RoamingFolder,您无需额外权限即可访问。您还可以声明 musicLibrary 等功能来访问库。

最后,您还可以让用户使用 FileOpenPickerFileSavePicker 类的对话框手动选择文件,然后使用 保留对该文件的授予访问权限>StorageApplicationPermissions.FutureAccessList - 了解更多信息 here .

有关详细信息,请参阅 docs .

关于c# - 如何显示本地文件夹uwp中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54091707/

相关文章:

c# - UWP中页面之间传递一些参数

c# - 变量赋值和读取是原子操作吗?

c# - 用于C#的构建工具,类似于Java的maven/gradle

c# - iTextSharp : Put blank space before a table

c# - 使用 SUM 和 IS NOT NULL 对 LINQ for MVC 进行 SQL 查询

c# - UWP:如果指针被按下,则指针移动

sockets - 将来自 Hololens 的麦克风语音输入发送到 PC

c# - LINQ 运算符根据通用分隔符将 double 列表拆分为多个 double 列表

javascript - 如何为 Window Store 构建 Electron 应用程序?

c# - 找不到类型或命名空间名称 'Description'