简体   繁体   English

UnauthorizedAccessException错误WindowsPhone C#

[英]UnauthorizedAccessException error WindowsPhone C#

    private async void  Button_Click(object sender, RoutedEventArgs e)
    {            
        await createFile();
        await readFile();
    }
    private async Task readFile()
    {
        StorageFolder local =  KnownFolders.PicturesLibrary;
        if (local != null)
        {
            var dataFolder = await local.GetFolderAsync("msgGen");

            var file = await dataFolder.OpenStreamForReadAsync("Msg.dat");

            StreamReader msg = new StreamReader(file);

            this.textblock.Text = msg.ReadLine();                
        }
    }
    private async Task createFile()
    {
        byte[] mensaje = System.Text.Encoding.UTF8.GetBytes("Este Mensaje".ToCharArray());

        StorageFolder local = KnownFolders.DocumentsLibrary;

        var dataFolder = await local.CreateFolderAsync("msgGen", CreationCollisionOption.ReplaceExisting);

        var file = await dataFolder.CreateFileAsync("Msg.dat", CreationCollisionOption.OpenIfExists);

        var s = await file.OpenStreamForWriteAsync();

        s.Write(mensaje,0,mensaje.Length);



    }

This code might be missing some { or } but it was accounted for.Anyways this is my code for when a button is clicked.I have done it using the example from MSDN and have tried some different things as you can see in the code. 这段代码可能缺少一些{或},但是却被解释了。无论如何,这是我单击按钮时的代码。我已经使用MSDN的示例进行了此操作,并尝试了一些不同的方法,如代码中所示。 Anyways my problem is that I have been getting this error and tried a lot of the things in this websites with no luck. 无论如何,我的问题是我一直遇到此错误,并在本网站上尝试了很多事情,但没有运气。 I think the error is because I don't have administrator rights but I honestly don't know what I am doing wrong. 我认为该错误是因为我没有管理员权限,但老实说我不知道​​自己在做什么错。 My question is how to get rid of the error I wrote in the title? 我的问题是如何摆脱标题中写的错误?

This was done for Windows Phone 8.1 in C#. 这是在C#中为Windows Phone 8.1完成的。 Thanks for all the help. 感谢您的所有帮助。

According to MSDN KnownFolders.DocumentsLibrary 根据MSDN KnownFolders.DocumentsLibrary

You can't use the Documents library in a Windows Phone Store app. 您不能在Windows Phone Store应用程序中使用文档库。

You can use your app's local folder. 您可以使用应用程序的本地文件夹。

StorageFolder local = ApplicationData.Current.LocalFolder;

And stream needs to be closed when you finish writing/reading. 完成写/读后,需要关闭流。

So you code can be fixed as below 所以你的代码可以固定如下

private async Task readFile()
{
    StorageFolder local = ApplicationData.Current.LocalFolder;
    if (local != null)
    {
        //...your code

        //Close the stream after using it
        msg.Close();
    }
}

private async Task createFile()
{
    //...your code
    StorageFolder local = ApplicationData.Current.LocalFolder;
    //...your code

    //Close the stream after using it
    s.Close();
}

First you should add the Capabilities of picturesLibrary into Package.appxmanifest file in sln: 首先,您应该将“ picturesLibrary的功能”添加到sln中的Package.appxmanifest文件中:

<Capabilities>
<Capability Name="internetClientServer" />
<Capability Name="picturesLibrary" />
</Capabilities>

then you also need add fileTypeAssociation into Application node which in Package.appxmanifest file: 那么您还需要将FileTypeAssociation添加到Package.appxmanifest文件中的Application节点中:

<Extensions>
    <Extension Category="windows.fileTypeAssociation">
      <FileTypeAssociation Name=".dat">
        <DisplayName>DatFile</DisplayName>
        <SupportedFileTypes>
          <FileType>.dat</FileType>
        </SupportedFileTypes>
      </FileTypeAssociation>
    </Extension>
  </Extensions>

and your code has some errors: 并且您的代码有一些错误:

1: create file in KnownFolders.DocumentsLibrary and read file in KnownFolders.PicturesLibrary? 1:在KnownFolders.DocumentsLibrary中创建文件并在KnownFolders.PicturesLibrary中读取文件?

2: Write stream has not disposed. 2:写流未处理。

fix your code like this: 像这样修复您的代码:

private async Task readFile()
    {
        StorageFolder local = KnownFolders.PicturesLibrary;
        if (local != null)
        {
            var dataFolder = await local.GetFolderAsync("msgGen");
            var file = await dataFolder.GetFileAsync("Msg.dat");
            var stream = (await file.OpenReadAsync()).AsStream();
            var msg = new StreamReader(stream);
            var text = msg.ReadLine();

            this.textBlock.Text = text;
            stream.Dispose();
        }
    }

private async Task createFile()
    {
        byte[] mensaje = System.Text.Encoding.UTF8.GetBytes("Este Mensaje".ToCharArray());

        var local = KnownFolders.PicturesLibrary;

        var dataFolder = await local.CreateFolderAsync("msgGen", CreationCollisionOption.ReplaceExisting);

        var file = await dataFolder.CreateFileAsync("Msg.dat", CreationCollisionOption.OpenIfExists);

        var s = await file.OpenStreamForWriteAsync();

        s.Write(mensaje, 0, mensaje.Length);

        s.Dispose();

    }

and test result is: 测试结果为:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM