当应用程序第一次运行时,我将项目添加到文本文件中,如下所示:
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 中的条目时,结果却很奇怪。最新条目出现在顶部,与列表中的第二位差距很大。在此之上添加另一个条目似乎隐藏了最后一个条目。
下面的截图应该是第一个“Chicago”条目之上的条目。应该有第二个芝加哥条目似乎也消失了。
有什么方法可以浏览到手机上创建的文本文件并查看数据的确切布局?这可以提供线索。
经过进一步调查,当我写另一行时,它会覆盖现有文本,而不是添加新行。知道如何阻止这种情况吗?