zoukankan      html  css  js  c++  java
  • (整理)streamWriter、streamReader与FileStream

          今天偶然使用VS代码分析,发现CA2000警告,然后其中一条为streamWriter、streamReader与FileStream相关内容,特查询并记录一下。

          引文地址:http://bbs.csdn.net/topics/390313584;

            http://blog.csdn.net/jaychouliyu/article/details/6152256

            实现代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
                //StreamReader sr = new StreamReader(fs);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write("Hi there");
                //注意:调用StreamWriter.Close会同时关闭其使用的FileStream对象,而FileStream对象不需要显示地关闭  
                //sw.Close();
                sw.Dispose();
                fs.Dispose();
                
                Thread.Sleep(4000);
                if(sw == null)
                {
                    Console.WriteLine("Null");
                }
                else
                {
                    Console.WriteLine("not Null");
                }
                Console.ReadLine();
            }
        }
    }

            先释放“sw”后释放“fs”时,确实会出现“对象未按所有异常路径释放”;如果先释放“fs”,后释放“sw”,出现“ObjectDisposeException”。“fs”与“sw”的确是互相关联,释放其中一个,会对另外一个造成一定的影响,

  • 相关阅读:
    Windows消息循环
    python 如何获得网卡的Ip地址
    curl 如何测量它花了多少时间?
    mininet 如何创建有不同带宽的链路
    Emacs学习笔记:多窗口操作
    RYU 如何扔掉一个符合要求的数据包
    RYU OFPMatch 的使用方法
    __attribute__如何使用的记录
    make file 和 GCC标志学习
    mininet and ovs 总结
  • 原文地址:https://www.cnblogs.com/hhhh2010/p/5210306.html
Copyright © 2011-2022 走看看