I have my program save the contents of 4 different text boxes into 4 separate text files using IsolatedStorageFile. This works fine, until I have to load them later. When I'm loading, all 4 text files are loaded into the first text box, and the remaining 3 are left blank.
Here's the code I'm using to save the files:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
defaultPicker.ItemsSource = new List<string>() { { box1.Text }, { box2.Text }, { box3.Text }, { box4.Text } };
//Deletes any previous saved files
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
storage.DeleteFile("ip-1.txt");
storage.DeleteFile("ip-2.txt");
storage.DeleteFile("ip-3.txt");
storage.DeleteFile("ip-4.txt");
// This saves users IP's into text files for later loading
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("ip-1.txt", FileMode.OpenOrCreate, fileStorage));
Writer.WriteLine(box1.Text);
new StreamWriter(new IsolatedStorageFileStream("ip-2.txt", FileMode.OpenOrCreate, fileStorage));
Writer.WriteLine(box2.Text);
new StreamWriter(new IsolatedStorageFileStream("ip-3.txt", FileMode.OpenOrCreate, fileStorage));
Writer.WriteLine(box3.Text);
new StreamWriter(new IsolatedStorageFileStream("ip-4.txt", FileMode.OpenOrCreate, fileStorage));
Writer.WriteLine(box4.Text);
Writer.Close();
}
and to load the files later:
private void Button_Click_4(object sender, RoutedEventArgs e)
{
// Loads IP's from text file or displays error message
//First clear the boxes...
box1.Text = "";
box2.Text = "";
box3.Text = "";
box4.Text = "";
//Load from text files...
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader Reader = null;
try
{
Reader = new StreamReader(new IsolatedStorageFileStream("ip-1.txt", FileMode.Open, fileStorage));
string textFile = Reader.ReadToEnd();
box1.Text = textFile;
}
catch
{
}
IsolatedStorageFile.GetUserStoreForApplication();
try
{
Reader = new StreamReader(new IsolatedStorageFileStream("ip-2.txt", FileMode.Open, fileStorage));
string textFile2 = Reader.ReadToEnd();
box2.Text = textFile2;
}
catch
{
}
IsolatedStorageFile.GetUserStoreForApplication();
try
{
Reader = new StreamReader(new IsolatedStorageFileStream("ip-3.txt", FileMode.Open, fileStorage));
string textFile3 = Reader.ReadToEnd();
box3.Text = textFile3;
}
catch
{
}
IsolatedStorageFile.GetUserStoreForApplication();
try
{
Reader = new StreamReader(new IsolatedStorageFileStream("ip-4.txt", FileMode.Open, fileStorage));
string textFile4 = Reader.ReadToEnd();
box4.Text = textFile4;
Reader.Close();
}
catch
{
}
// To sync with ListPicker
defaultPicker.ItemsSource = new List<string>() { { box1.Text }, { box2.Text }, { box3.Text }, { box4.Text } };
}
Any help into the right direction would be appreciated.