使用IsolatedStorageFile将数据添加到现有文本文件

时间:2014-05-19 20:02:03

标签: c# windows-phone-8 streamwriter listpicker isolatedstoragefile

当应用程序第一次运行时,我将项目添加到文本文件中,如下所示:

     StringBuilder sb = new StringBuilder();                                 // Use a StringBuilder to construct output.
var store = IsolatedStorageFile.GetUserStoreForApplication();           // Create a store
store.CreateDirectory("testLocations");                                 // Create a directory
IsolatedStorageFileStream rootFile = store.CreateFile("locations.txt"); // Create a file in the root.
rootFile.Close();                                                       // Close File
string[] filesInTheRoot = store.GetFileNames();                         // Store all files names in an array
Debug.WriteLine(filesInTheRoot[0]);                                     // Show first file name retrieved (only one stored at the moment)

string filePath = "locations.txt";

                    if (store.FileExists(filePath)) {

                        Debug.WriteLine("Files Exists"); 
                        StreamWriter sw =
                                new StreamWriter(store.OpenFile(filePath,
                                    FileMode.Open, FileAccess.Write));

                                Debug.WriteLine("Writing..."); 
                                sw.WriteLine("Chicago, IL;");
                                sw.WriteLine("Chicago, IL (Q);");
                                sw.WriteLine("Dulles, VA;");
                                sw.WriteLine("Dulles, VA (Q);");
                                sw.WriteLine("London, UK;");
                                sw.WriteLine("London, UK (Q);");
                                sw.WriteLine("San Jose, CA;");
                                sw.WriteLine("San Jose, CA (Q);");
                                sw.Close();
                                Debug.WriteLine("Writing complete"); 

                            }

然后,当按下按钮时,我可以使用以下代码在ListPicker中查看此数据,同样一切正常:

 private void locChoice(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            string filePath = "locations.txt";

            if (store.FileExists(filePath))
            { 
               Debug.WriteLine("Files Exists");
                try
                {
                    string fileData;
                    using (IsolatedStorageFileStream isoStream =
                        new IsolatedStorageFileStream("locations.txt", FileMode.Open, store))
                    {
                        using (StreamReader reader = new StreamReader(isoStream))
                        {
                            fileData = reader.ReadToEnd();
                        }
                    }
                    testLocationPicker.ItemsSource = fileData.Split(';');
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

            testLocationPicker.Open();
        }

我现在想以编程方式添加到文本文件中,一旦用户填写了一些文本块,我就这样做了:

 StringBuilder sb = new StringBuilder();                                 // Use a StringBuilder to construct output.
            var store = IsolatedStorageFile.GetUserStoreForApplication();           // Create a store
            string[] filesInTheRoot = store.GetFileNames();                         // Store all files names in an array
            Debug.WriteLine(filesInTheRoot[0]);                                     // Show first file name retrieved (only one stored at the moment)

            string filePath = "locations.txt";

            if (store.FileExists(filePath))
            {

                Debug.WriteLine("Files Exists");
                StreamWriter sw =
                        new StreamWriter(store.OpenFile(filePath,
                            FileMode.Open, FileAccess.Write));

                Debug.WriteLine("Writing...");
                sw.WriteLine(locationName + ";");   // Semi Colon required for location separation in text file
                sw.Close();
                Debug.WriteLine(locationName + "; added");
                Debug.WriteLine("Writing complete");

            } 

然而,当我现在回到上一页并单击按钮查看ListPicker中的条目时,结果很奇怪。最新条目显示在顶部,与列表中的第二位有很大差距。在其上添加另一个条目似乎隐藏了最后一个条目。

下面的屏幕截图应该包含第一个“芝加哥”条目上方的条目。应该有第二个芝加哥参赛作品似乎也已经消失了。

有没有办法浏览手机上创建的文本文件并确切了解数据的布局方式?这可以提供线索。

enter image description here

经过进一步调查后,当我写另一行时,它会覆盖现有文本,而不是添加新行。知道如何制止这个吗?

1 个答案:

答案 0 :(得分:0)

我发现了一些与附加文件相关的文档,这证实了我怀疑数据被覆盖了。

现在添加数据时使用的方法如下所示:

// ----------------------------------------------------------------
            // Write the friend name to the locations text file upon submission
            // ----------------------------------------------------------------

            StringBuilder sb = new StringBuilder();                                 // Use a StringBuilder to construct output.
            var store = IsolatedStorageFile.GetUserStoreForApplication();           // Create a store
            string[] filesInTheRoot = store.GetFileNames();                         // Store all files names in an array
            Debug.WriteLine(filesInTheRoot[0]);                                     // Show first file name retrieved (only one stored at the moment)
            byte[] data = Encoding.UTF8.GetBytes(locationName + ";");
            string filePath = "locations.txt";

            if (store.FileExists(filePath))
            {
                using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Append, store))
                {

                    Debug.WriteLine("Writing...");
                    stream.Write(data, 0, data.Length);   // Semi Colon required for location separation in text file
                    stream.Close();
                    Debug.WriteLine(locationName + "; added");
                    Debug.WriteLine("Writing complete");
                }


            }

另一个Stack Overflow帖子帮助我解决了第一次搜索时没有出现的问题:

Does IsolatedStorage File.Append mode has an error with the the '\n'?