storageFolder.CreateFileAsync期间的UnauthorizedAccessException

时间:2012-10-25 02:50:19

标签: c# windows-8 windows-store-apps microsoft-metro

我有以下代码

    // 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启用了所有权限。但是,我不确定为什么在UnauthorizedAccessException期间我仍然会收到storageFolder.CreateFileAsync

我错过了其他任何事情?

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

根据您上面的评论,我假设您已经检查了package.appxmanifest中的“文档库”功能。

查看documentation for StorageFolder.CreateFileAsync中的评论。它说:“如果您尝试在库或文件组等虚拟文件夹中创建文件,则此方法可能会失败。”然后在“示例”部分中,它显示了使用Windows.Storage.KnownFolders.documentsLibrary.createFileAsync()在文档库中创建新文件的另一种方法。你能尝试一下,看看它是否有效?

相关问题