0

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.

4

2 回答 2

1

首先,我建议你停止吃所有的例外。至少在调试模式下,在 pragma 中有空的 catch 块。

我会删除 Reader 的单个声明,并为每个文件创建一个新声明:

try
{
    using (StreamReader reader = new StreamReader(new IsolatedStorageFileStream("ip-1.txt", FileMode.Open, fileStorage))) 
    {
        string textFile = reader.ReadToEnd();
        box1.Text = textFile;
    }
}
于 2013-07-08T23:51:24.787 回答
0

您没有正确写入四个文件。您Writer每次都需要重新分配。

    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();

将来你应该在你的 catch 块中有一些东西来显示任何捕获的异常。在这种情况下,它会告诉您其他三个文件无法加载,因为它们不存在。

于 2013-07-08T23:58:05.353 回答