Hi,
My program do 2 tasks:
- Write a string to file ( for example: "JohnScott2111111")
- Read content from that file to a string ( for example: "JohnScott2111111")
I only store Name + phone number, so that string variable is short ( small).
The problem is:
if I write, then read: it's ok.
but (If I write, then read) AGAIN, it's fail. Compiler tell me: it cannot open file to READ ( file is still opened to WRITE?)
I think I have to close ( or something like that) after write, or read a file? but how to do that?
- by the way, where is "ApplicationData.Current.LocalFolder"? Will my file exist after my app closed? I mean, will data be deleted after user close program?
Thanks so much!
here are 2 methods I used to load and save text to file:
//save- write
private asyncvoid saveText(string filename)
{
StorageFolderstorageFolder= ApplicationData.Current.LocalFolder;
IStorageFilefile = await storageFolder.CreateFileAsync(filename,CreationCollisionOption.ReplaceExisting);
intx = 0;
try
{
if (file != null)
{
x++;
await FileIO.WriteTextAsync(file, "Pikachu"+x.ToString());
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle file not found
}
}
//read - load
private async void loadText(string filename)
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
IStorageFile file = await storageFolder.GetFileAsync(filename);
try
{
if (file != null)
{
string text = await Windows.Storage.FileIO.ReadTextAsync(file);
btnHelp.Content = text;
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle file not found
}
}