Arrays 对数组进行排序,该数组由IsolatedStorageFile填充。文本文件似乎会移动0项
我有下面的代码,它将文本文件从IsolatedStorage中拆分,用数据填充数组,对其进行排序,然后将其指定为ListPicker的源:Arrays 对数组进行排序,该数组由IsolatedStorageFile填充。文本文件似乎会移动0项,arrays,sorting,windows-phone-8,split,isolatedstorage,Arrays,Sorting,Windows Phone 8,Split,Isolatedstorage,我有下面的代码,它将文本文件从IsolatedStorage中拆分,用数据填充数组,对其进行排序,然后将其指定为ListPicker的源: var splitFile = fileData.Split(';'); string[] testArray = splitFile; Array.Sort<string>(testArray); testLocationPicker.ItemsSource = testArray; 然后,当我添加到文件时,我会执行以下操作: String
var splitFile = fileData.Split(';');
string[] testArray = splitFile;
Array.Sort<string>(testArray);
testLocationPicker.ItemsSource = testArray;
然后,当我添加到文件时,我会执行以下操作:
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");
}
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");
}
}
我正在使用“;”进行拆分,这可能是个问题吗?排序没有问题:“空格”被认为位于“a”之前,因此它出现在列表的顶部。真正的问题是:为什么一开始就有一个空条目 我的猜测是,在创建文件时,您使用
分隔每个条目代码>,包括最后一个。因此,当使用string.Split
方法解析数据时,在数组末尾会留下一个空条目
防止这种情况的一种简单方法是使用过滤空条目的string.Split
方法重载:
var splitFile = fileData.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
我采用了另一种方式,使用IsolatedStorage设置和存储阵列/列表来完成我想要的任务。我会尝试一下,然后再回复您,感谢您抽出时间回答我。我已经更新了我上面的问题,并提供了更多详细信息。我正在使用;,即使是在最后一个条目上。我想根据需要添加到文件并从文件中删除。排序似乎不是按字母顺序排序,正如您所看到的,即使使用空数组值,它仍然将Chicago放在底部,而它应该放在顶部。
var splitFile = fileData.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);