1

我必须在会话之间存储控件的内容。这就是我所做的:

    private void Window_Closing(object sender, CancelEventArgs e)
    {
        IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly();
        using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream("somefilename", FileMode.Create, f))
        using(StreamWriter writer = new StreamWriter(stream))
        {
            foreach(string path in searchFoldersListView.Items)
            {
                writer.WriteLine(path);
            }
        }
    }

这是附加到Closing窗口的事件。调试时,控件遍历所有项目,但是当我再次运行程序时,控件为空。事实证明,当我导航到该文件时,它是空的。当我通过将 a 放入 foreach 循环中更改代码以保存到常规文件File.AppendAllText()时,该文件保存得很好。这是为什么?

编辑:阅读事件处理程序:

    private void Window_Initialized(object sender, EventArgs e)
    {
        IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly();
        using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream("somefilename", FileMode.OpenOrCreate, f))
        using(StreamReader reader = new StreamReader(stream))
        {
            string line = reader.ReadLine();
            while(line != null)
            {
                searchFoldersListView.Items.Add(line);
            }
        }
    }
4

1 回答 1

0

您的方法中有一个从文件读取的错误。您只阅读了第一行,可能是null. 尝试以下操作:

    using(StreamReader reader = new StreamReader(stream))
    {
        string line = reader.ReadLine();
        while(line != null)
        {
            searchFoldersListView.Items.Add(line);
            line = reader.ReadLine();
        }
    }
于 2012-12-08T00:04:02.410 回答