Question

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.

Was it helpful?

Solution 2

You aren't writing to the four files correctly. You need to reassign your Writer each time.

    StreamWriter Writer = new StreamWriter(new IsolatedStorageFileStream("ip-1.txt", FileMode.OpenOrCreate, fileStorage));
    Writer.WriteLine(box1.Text);

    Writer = new StreamWriter(new IsolatedStorageFileStream("ip-2.txt", FileMode.OpenOrCreate, fileStorage));
    Writer.WriteLine(box2.Text);

    Writer = new StreamWriter(new IsolatedStorageFileStream("ip-3.txt", FileMode.OpenOrCreate, fileStorage));
    Writer.WriteLine(box3.Text);

    Writer = new StreamWriter(new IsolatedStorageFileStream("ip-4.txt", FileMode.OpenOrCreate, fileStorage));
   Writer.WriteLine(box4.Text);

    Writer.Close();

In the future you should have something in your catch blocks to display any caught exceptions. In this case it would have told you that the three other files weren't able to be loaded because they didn't exist.

OTHER TIPS

First, I suggest you stop eating all exceptions. At least in debug mode, have the empty catch blocks in a pragma.

I would drop the single declaration of Reader and create a new one per file:

try
{
    using (StreamReader reader = new StreamReader(new IsolatedStorageFileStream("ip-1.txt", FileMode.Open, fileStorage))) 
    {
        string textFile = reader.ReadToEnd();
        box1.Text = textFile;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top