c期间发生UnauthorizedAccessException

c期间发生UnauthorizedAccessException

本文介绍了storageFolder.CreateFileAsync期间发生UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一段代码

    // Point to c:\users\yancheng\documents\visual studio 2012\Projects\App5\App5\bin\x86\Debug\AppX
    StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

    StorageFile file = await storageFolder.CreateFileAsync("1000.txt");

    if (file != null)
    {
        using (IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (DataWriter dataWriter = new DataWriter(writeStream))
            {
                dataWriter.WriteInt32(1000);
            }
        }
    }

我已通过Package.appxmanifest启用了所有权限.但是,我不确定为什么在storageFolder.CreateFileAsync期间仍然得到UnauthorizedAccessException.

I have enabled all permission through Package.appxmanifest. However, I am not sure why I am still getting UnauthorizedAccessException during storageFolder.CreateFileAsync.

还有其他我错过的事情吗?

Any other things I had missed out?

推荐答案

您正在尝试在应用程序的安装文件夹中创建文件.此位置是只读位置(请参见 http://msdn .microsoft.com/en-us/library/windows/apps/hh967755.aspx ).

You are trying to create a file in the installation folder of the app. This location is a read-only location (see http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx).

如果该应用程序是由Visual Studio安装的,则有可能将文件奇怪地复制到此文件夹并从中删除文件,这是我在Microsoft Connect中报告的错误.总之,所有应用程序都不能更改安装文件夹的任何内容.

Strangely copying files to this folder and deleting files from it is possible if the app was installed by Visual Studio, what I reported as a bug in Microsoft Connect. All in all Apps should not be able to change any content of the installation folder.

仅允许应用程序写入通过ApplicationData和KnownFolders到达的文件夹.在后一种情况下,必须声明相应的功能和文件类型.

Apps are only allowed to write to the folders reached through ApplicationData and the KnownFolders. In latter case corresponding capabilities and file types must be declared.

这篇关于storageFolder.CreateFileAsync期间发生UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:52