C# Using StreamReader

=== Program that uses StreamReader [C#] ===

using System;
using System.IO;

class Program
{
    static void Main()
    {
        //
        // It will free resources on its own.
        //
        string line;
        using (StreamReader reader = new StreamReader("file.txt"))
        {
            line = reader.ReadLine();
        }
        Console.WriteLine(line);
    }
}

=== Program that uses Dispose [C#] ===

using System;
using System.IO;

class Program
{
    static void Main()
    {
        //
        // Read a line from a file the old way.
        //
        StreamReader reader = new StreamReader("file.txt");
        string line = reader.ReadLine();
        reader.Close();
        // You should call Dispose on 'reader' here, too.
        reader.Dispose();
        Console.WriteLine(line);
    }

}

 

  Why use using?  参照http://ryanfarley.com/blog/archive/2004/03/18/447.aspx 中作者解释

 

Even without understanding MSIL, you can clearly see what is going on here. The code was essentially translated to a try/finally block calling Dispose in the finally. If I were to take this same IL and convert it back to C# using Reflector then it would be exactly that, a try/finally block where Dispose is called in the finally.

So why use using at all? If all using does is translate to a try/finally block then why would you want to use it? Well, as I said at the beginning, it really does come down to a matter of preference. I think the code is more elegant but others may not and that is OK. However, take any peice of code that does not use using and tell me that Dispose is being called without looking in the finally block. You can't. There's no way you can guarantee that Dispose is being called without looking in the finally block to see. With using, there's nothing to look at. You know it is being called. You know that there's no way you could have forgotten to call Dispose and left some references to unmanaged resources out there somewhere. You know this because it is done automatically for you - and I think that is just cool.

To finish things up, it is important to understand that since the purpose of using is to create a scope for an object where Dispose is automatically called upon leaving the scope that this requires that the object actually implements IDisposable. Using an object that does not implement IDisposable will result in an error, just as manually calling Dispose for the object in a finally block would result in an error. For a list of objects in the framework that implement IDisposable you can look at the IDisposable definition in the docs. However, with that said, you should also implement IDisposable in your own classes that use unmanaged resources such as interop etc.

 

google翻译

 

即使不理解的MSIL,你可以清楚地看到这里发生了什么。该代码基本上转化为一个try / finally块中调用最终处置。如果我要采取同样的IL和它转换到C#中使用反射镜,然后便正是这样一个try / finally块丢弃在这里终于被调用。

那么,为什么用使用呢?如果全部采用所做的是翻译成一个try / finally块那你为什么要使用它?嗯,正如我在开始时说,它确实可以归结为一个见仁见智的问题。我认为代码更优雅,但其他人可能不会,这是确定。但是,采取任何的代码使用和不使用没有告诉在finally块找我,被调用Dispose的一块。你不能。有没有办法可以保证不被丢弃在finally块来看看寻找调用。与使用,有什么可看。你知道这是被调用。你知道,有没有你可能忘记调用Dispose,离开那里有些地方的非托管资源的引用方式。你知道这是因为它是为您自动完成 - 我认为这只是冷静。

要完成的事情了,重要的是要明白,自从使用目的是建立一个为在离开时自动Dispose的范围,这需要该对象实际实现IDisposable称为对象的范围。使用对象不实现IDisposable将导致一个错误,就像手动finally块中调用该对象的Dispose将导致错误。如需在框架中实现IDisposable对象的列表,你可以看看在docs IDisposable接口的定义。然而,随着中说,你也应该在自己的类实现IDisposable接口,使用非托管资源互操作等,

 


Server.MapPath() 探究 (转)
Server.MapPath(path)
       The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
Parameters (MSDN:ms-help://MS.MSDNQTR.2003FEB.2052/iisref/htm/ref_vbom_serommp.htm)
用法:
1.Server.MapPath("/")  应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\
2.Server.MapPath("./")  表示所在页面的当前目录
    注:等价于Server.MapPath("")  返回 Server.MapPath("")所在页面的物理文件路径
3.Server.MapPath("../")表示上一级目录
4.Server.MapPath("~/")表示当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置 如:C:\Inetpub\wwwroot\Example\
注:等效于Server.MapPath("~")。

不知道是否正确,研究中......
另:以下几句等效
string filename=Server.MapPath("./") + @"\Web.config";
string filename=Server.MapPath("./") + "/Web.config";
string filename=Server.MapPath("") + @"\Web.config"

附一例子:修改web.config的某一节点的属性值
    public void write()
        {
            string key1 = this.TextBox1.Text;
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("")+"/web.config");
            // 不是Tables[0]
            ds.Tables[1].Rows[0][1] = key1;
            ds.AcceptChanges();
            ds.WriteXml(Server.MapPath("")+"/web.config");
            ds.Dispose();           
        }

 

转载于:https://www.cnblogs.com/gym_sky/archive/2011/01/12/1933483.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.1 什么是Stream? 1.2 什么是字节序列? 1.3 Stream的构造函数 1.4 Stream的重要属性及方法 1.5 Stream的示例 1.6 Stream异步读写 1.7 Stream 和其子类的类图 2.1 为什么要介绍 TextReader? 2.2 TextReader的常用属性和方法 2.3 TextReader 示例 2.4 从StreamReader想到多态 2.5 简单介绍下Encoding 编码 2.6 StreamReader 的定义及作用 2.7 StreamReader 类的常用方法属性 2.8 StreamReader示例 3.1 为何介绍TextWriter? 3.2 TextWriter的构造,常用属性和方法 3.3 IFormatProvider的简单介绍 3.4 如何理解StreamWriter? 3.5 StreamWriter属性 3.6 StreamWriter示例 4.1 如何去理解FileStream? 4.2 FileStream的重要性 4.3 FileStream常用构造函数(重要) 4.4 非托管参数SafeFileHandle简单介绍 4.5 FileStream常用属性介绍 4.6 FileStream常用方法介绍 4.7 FileStream示例1:*文件的新建和拷贝(主要演示文件同步和异步操作) 4.8 FileStream示例2:*实现文件本地分段上传 5.1 简单介绍一下MemoryStream 5.2 MemoryStream和FileStream的区别 5.3 通过部分源码深入了解下MemoryStream 5.4 分析MemorySteam最常见的OutOfMemory异常 5.5 MemoryStream 的构造 5.6 MemoryStream 的属性 5.7 MemoryStream 的方法 5.8 MemoryStream 简单示例 : XmlWriter中使用MemoryStream 5.9 MemoryStream 简单示例 :自定义一个处理图片的HttpHandler 6.1 简单介绍一下BufferedStream 6.2 如何理解缓冲区? 6.3 BufferedStream的优势 6.4 从BufferedStream 中学习装饰模式 6.5 如何理解装饰模式 6.6 再次理解下装饰模式在Stream中的作用 6.7 BufferedStream的构造 6.8 BufferedStream的属性 6.9 BufferedStream的方法 6.10 简单示例:利用socket 读取网页并保存在本地 7.1 NetworkStream的作用 7.2 简单介绍下TCP/IP 协议和相关层次 7.3 简单说明下 TCP和UDP的区别 7.4 简单介绍下套接字(Socket)的概念 7.5 简单介绍下TcpClient,TcpListener,IPEndPoint类的作用 7.6 使用NetworkStream的注意事项和局限性 7.7 NetworkStream的构造 7.8 NetworkStream的属性 7.9 NetworkStream的方法 7.10 NetwrokStream的简单示例 7.11 创建一个客户端向服务端传输图片的小示例 版权归作者所有,仅供学习参考

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值