Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处置在另一个对象的构造函数中使用的对象_C#_.net_Oop_Dispose_Using - Fatal编程技术网

C# 处置在另一个对象的构造函数中使用的对象

C# 处置在另一个对象的构造函数中使用的对象,c#,.net,oop,dispose,using,C#,.net,Oop,Dispose,Using,我使用这段代码从一个独立的存储器中读取值 IsolatedStorageFile isoStore = null; StreamReader reader = null; IsolatedStorageFileStream isolatedStorageFileStream = null; String strIsolatedStorageValue = string.Empty; isoStore = Isola

我使用这段代码从一个独立的存储器中读取值

        IsolatedStorageFile isoStore = null;
        StreamReader reader = null;
        IsolatedStorageFileStream isolatedStorageFileStream = null;
        String strIsolatedStorageValue = string.Empty;

        isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

        try
        {
            isolatedStorageFileStream = new IsolatedStorageFileStream(strKey + ".txt", FileMode.OpenOrCreate, isoStore);
            // This code opens the store and reads the string.
            reader = new StreamReader(isolatedStorageFileStream);

            // Read a line from the file and add it to sb.
            strIsolatedStorageValue = reader.ReadLine();
        }
        catch (Exception)
        {

        }
        finally
        {
            if (isolatedStorageFileStream != null)
            {
                isolatedStorageFileStream.Dispose();
            }
            if (reader != null)
            {
                reader.Dispose();
            }
        }

        // Return the string.
        return strIsolatedStorageValue;
问题是,当我处理isolatedStorageFileStream,然后处理读取器时,VisualStudio告诉我isolatedStorageFileStream可以被多次处理!当不处理它时,我得到的警告是应该首先处理isolatedStorageFileStream

在这种情况下该怎么做,即处理另一个一次性对象的构造函数中使用的对象


谢谢使用
使用
关键字:

using (IsolatedStorageFileStream isolatedStorageFileStream = 
       new IsolatedStorageFileStream(
            strKey + ".txt", FileMode.OpenOrCreate, isoStore))
using (StreamReader reader = new StreamReader(isolatedStorageFileStream))
{
    // Read a line from the file and add it to sb.
    strIsolatedStorageValue = reader.ReadLine();
}

return strIsolatedStorageValue;

使用
为您安全地调用
Dispose
,您不必手动调用它。

您应该在文件流之前处理读取器

为了简化代码,您应该使用
using
块。他们会自动为您执行try/finally/dispose模式:

using (isolatedStorageFileStream = new IsolatedStorageFileStream(
          strKey + ".txt", FileMode.OpenOrCreate, isoStore)) {
    // This code opens the store and reads the string.
    using (reader = new StreamReader(isolatedStorageFileStream)) {
        strIsolatedStorageValue = reader.ReadLine();
   }
}
最后会自动尝试为您提供IDisposable(如果不为null,则丢弃)


谢谢:),但我仍然收到来自visual studio的如下警告:警告5 CA2202:Microsoft。用法:对象“isolatedStorageFileStream”可以在方法“IsolatedStorageReader.ReadFromIsolatedStorage(string)”中多次释放。为了避免生成System.ObjectDisposedException,您不应该对Objects多次调用Dispose,这只是一个fxcop警告。有关详细信息,请参见此答案:顺便说一句,您可以使用装饰StreamReader类实例,以防止StreamReader类实例关闭流。
using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(strKey + ".txt", FileMode.OpenOrCreate, isoStore))
{
    using (StreamReader reader = new StreamReader(isolatedStorageFileStream))
    {
        string strIsolatedStorageValue = reader.ReadLine();
        return strIsolatedStorageValue;
    }
}