locked
Weirdness reading text from file in Windows.ApplicationModel.Package.Current.InstalledLocation

    Question

  • I'm trying to deploy a text file with my universal application, and at runtime read a string from it and parse it to an enum value.  I can't get it to work as shown in the following code.  The code shows that the approach does work with a file I write at runtime.

                // elsewhere:
                // using Windows.Storage;
                // using Windows.Storage.Streams;
                // enum Animal { cat, dog, zebra };

                string filename = "test.txt";
                string contents = "cat";

                // write the string "cat" to a file in LocalFolder
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFile textFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (DataWriter textWriter = new DataWriter(textStream))
                    {
                        textWriter.WriteString(contents);
                        await textWriter.StoreAsync();
                    }
                }

                contents = string.Empty;
                textFile = await localFolder.GetFileAsync(filename);

                // read the string "cat" from the file in LocalFolder
                using (IRandomAccessStream textStream = await textFile.OpenReadAsync())
                {
                    using (DataReader textReader = new DataReader(textStream))
                    {
                        uint textLength = (uint)textStream.Size;
                        await textReader.LoadAsync(textLength);
                        contents = textReader.ReadString(textLength);

                        // Asserts true
                        System.Diagnostics.Debug.Assert(string.Compare(contents, "cat") == 0);

                        // Writes True
                        System.Diagnostics.Debug.WriteLine(Enum.IsDefined(typeof(Animal), contents));

                        // Writes True
                        System.Diagnostics.Debug.WriteLine(Enum.IsDefined(typeof(Animal), "cat"));

                        // Sets enum as expected
                        Animal cat = (Animal)Enum.Parse(typeof(Animal), contents, true);
                    }
                }

                // read the string "cat" from the file in InstalledLocation
                contents = string.Empty;
                textFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(filename);

                using (IRandomAccessStream textStream = await textFile.OpenReadAsync())
                {
                    using (DataReader textReader = new DataReader(textStream))
                    {
                        uint textLength = (uint)textStream.Size;
                        await textReader.LoadAsync(textLength);
                        contents = textReader.ReadString(textLength);

                        // Asserts true
                        System.Diagnostics.Debug.Assert(string.Compare(contents, "cat") == 0);

                        // Writes False
                        System.Diagnostics.Debug.WriteLine(Enum.IsDefined(typeof(Animal), contents));

                        // Writes True
                        System.Diagnostics.Debug.WriteLine(Enum.IsDefined(typeof(Animal), "cat"));

                        // Throws argument exception
                        Animal cat = (Animal)Enum.Parse(typeof(Animal), contents, true);
                    }
                }

    Thursday, May 22, 2014 8:13 PM

Answers

  • Hi,

    I test your project. That's may be a bug in windows store app. I recommend you can edit your code in below:

     string contents = string.Empty;
                Windows.Storage.StorageFile textFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(filename);
                contents = await Windows.Storage.FileIO.ReadTextAsync(textFile);
                // Asserts true
                System.Diagnostics.Debug.Assert(string.Compare(contents, "cat") == 0);
    
                // Writes False
                System.Diagnostics.Debug.WriteLine(Enum.IsDefined(typeof(Animal), contents));
    
                // Writes True
                System.Diagnostics.Debug.WriteLine(Enum.IsDefined(typeof(Animal), "cat"));
    
                 Animal cat = (Animal)Enum.Parse(typeof(Animal), contents, true);
            
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, May 23, 2014 2:59 AM

All replies

  • Hi,

    I test your project. That's may be a bug in windows store app. I recommend you can edit your code in below:

     string contents = string.Empty;
                Windows.Storage.StorageFile textFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(filename);
                contents = await Windows.Storage.FileIO.ReadTextAsync(textFile);
                // Asserts true
                System.Diagnostics.Debug.Assert(string.Compare(contents, "cat") == 0);
    
                // Writes False
                System.Diagnostics.Debug.WriteLine(Enum.IsDefined(typeof(Animal), contents));
    
                // Writes True
                System.Diagnostics.Debug.WriteLine(Enum.IsDefined(typeof(Animal), "cat"));
    
                 Animal cat = (Animal)Enum.Parse(typeof(Animal), contents, true);
            
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, May 23, 2014 2:59 AM
  • Thank you, your approach somehow addresses the issue. Much appreciated.
    Friday, May 23, 2014 5:16 AM