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. 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#. Thanks for all the help.
According to MSDN KnownFolders.DocumentsLibrary
You can't use the Documents library in a Windows Phone Store app.
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:
<Capabilities>
<Capability Name="internetClientServer" />
<Capability Name="picturesLibrary" />
</Capabilities>
then you also need add fileTypeAssociation into Application node which in Package.appxmanifest file:
<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?
2: Write stream has not disposed.
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.