1

FileIO.ReadTextAsync always returns empty string ("") in Windows 8 application. Am working on Universal application and to note is that, FileIO.ReadTextAsync returns string when deploying in Windows Phone application but returns empty string in Windows 8 application? Here is my code.

async Task<string> LoadStatus(String listFileName)
{
    StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync(listFileName);
    string configData = await FileIO.ReadTextAsync(localFile);
    return configData;
}

And am calling this function as,

string result = await LoadStatus(AppConstants.VeryHappyStatusListName);

And listFileName is "FileName.xml".

I have gone through these (post1 and post2) posts and followed the same procedure but still returns null?

Also one thing to note that, Am getting error while writing data. Sometime it writes data perfectly and sometimes i get error while writing.

Here is my save to storage method.

async public void SaveStatus(List<Datum> list, String listFileName)
        {
            try
            {
                string localData = ObjectSerializer<List<Datum>>.ToXml(list);

                if (!string.IsNullOrEmpty(localData))
                {
                    StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(listFileName, CreationCollisionOption.ReplaceExisting);

                    if (localFile != null)
                    {
                        //await Task.Delay(3000);
                        await FileIO.WriteTextAsync(localFile, localData);
                    }
                }
            }
            catch (Exception ex)
            {
                DebugPrint(ex.Message.ToString());
                DebugPrintLocation("SaveStaus", "SessionStorage");
            }
        }

Am following this link : https://code.msdn.microsoft.com/windowsapps/CSWinStoreAppSaveCollection-bed5d6e6

Also important thing to note that, if I check each line using breakpoints everything goes fine. I can able to save large list and retrieve. But couldn't control without breakpoints.

Community
  • 1
  • 1
Balasubramani M
  • 7,742
  • 2
  • 45
  • 47
  • 1
    Open the file in Notepad (localFile.Path) to check if it is really an empty file? – kennyzx Oct 06 '14 at 09:32
  • @kennyzx Yeah, I will check that. But how could it run well in Windows Phone. Same code for both platforms but I dont know why it returns empty string – Balasubramani M Oct 06 '14 at 10:30
  • How are you including the file in the projects? Is it different? – Jeff Sanders - MSFT Oct 08 '14 at 13:12
  • No, Its normal Save method. I just followed the below link and completed. http://code.msdn.microsoft.com/windowsapps/CSWinStoreAppSaveCollection-bed5d6e6 Interesting thing to note here is that, am getting the response when my internet connection is fast in Windows 8 while saving the value. – Balasubramani M Oct 08 '14 at 13:28
  • StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("localData.txt", CreationCollisionOption.ReplaceExisting); This line in Save function returns string value when i have proper net connection and returns null when slow connection. And everything is working fine in Windows Phone. – Balasubramani M Oct 08 '14 at 13:30

2 Answers2

0

I use this in my Windows 8 apps and it works well:

    public async void ReadTextFile(string cFile)
    {
        var folder = KnownFolders.PicturesLibrary;
        var file = await folder.GetFileAsync(cFile);
        var read = await FileIO.ReadTextAsync(file);
        CurrentFileBuffer = read;
    }
Dick
  • 433
  • 5
  • 13
0

This should work for writing. It is also probably a good idea to explain which error you get exactly when you post a question.

public async Task WriteDataToFileAsync(string fileName, string content)
{
    byte[] data = System.Text.Encoding.Unicode.GetBytes(content);
    var folder = KnownFolders.PicturesLibrary;
    var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
    using (var s = await file.OpenStreamForWriteAsync())
    {
        await s.WriteAsync(data, 0, data.Length);
    }
}
Dick
  • 433
  • 5
  • 13