When the app is first ever run I write some defaults to a text file in IsolatedStorageFile, as below:
if (!settings.Contains("firstrun"))
{
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)");
Debug.WriteLine("Writing complete");
}
}
This appears to work, but now I need to populate my ListPicker with the contents of this text file, in alphabetical order.
What is the best way to do that? Create a list from the text file and then populate the ListPicker with the list?
Thanks to @har07 for confirming my thinking. I launch a full screen ListPicker from a button like so:
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();
}
I will work on the ordering as per @Pantelis suggestion.