0

我正在用 WP7 编写我的第一个应用程序,但我对 IS 有一个奇怪的问题:

                case "pn_s":            
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
                //IsolatedStorageFileStream isoStream = isoStorage.CreateFile("plan_zajec_pn.txt"); 
                IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(("plan_zajec_pn.txt"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isoStorage);
                StreamWriter writer = new StreamWriter(isoStream);
                string dane="";
                for (int i = 0; i < 10; i++)
                {
                    dane += godzina[i].Text;
                    dane += ";";
                    dane += przedmiot[i].Text;
                    dane += ";";
                    dane += sala[i].Text;
                    dane += ";";
                }
                writer.WriteLine(dane);
                isoStream.Close();
                break;

实际上它不起作用......我重新打开应用程序后创建文件但它是空的,我检查了“dane”并且它包含日期

4

1 回答 1

0

使用 aStreamWriter时,不能关闭底层流,否则编写器可能没有足够的时间写入所有数据。只需关闭编写器,它就会负责关闭流。using此外,即使抛出错误,您也应该将流包含在语句中以关闭它们。

最后,您的代码应如下所示:

case "pn_s":            
    using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var isoStream = new IsolatedStorageFileStream(("plan_zajec_pn.txt"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isoStorage))
        {
            using (var writer = new StreamWriter(isoStream))
            {
                string dane = "";

                for (int i = 0; i < 10; i++)
                {
                    dane += godzina[i].Text;
                    dane += ";";
                    dane += przedmiot[i].Text;
                    dane += ";";
                    dane += sala[i].Text;
                    dane += ";";
                }

                writer.WriteLine(dane);
            }
        }
    }

    break;
于 2013-04-02T12:50:18.577 回答