这些资料是我学习的时候在网络上寻找到的,发布出来希望对大家有帮助,一起努力前进,嘿嘿......Microsoft C#规范 2.0 版 GFC用户提交

feedsky 抓虾 pageflakes google reader my yahoo bloglines 鲜果 有道 http://wap.feedsky.com/bliplink

FileStream类型重写Dispose方法

文件流释放句柄后,才可以再对文件进行删除操作
---------------------------------------------------------------------------------------------
#define Version1
using System;
using System.IO;

//范例:FileStream类型派生自实现了Close方法和无参的IDisposable.Dispose方法的Stream类型,
//  FileStream类型简单重写了Dispose方法,它接受一个Boolean参数来释放Handle字段包装的非托管文件资源。

public sealed class Program
{
#if Version1
   public static void Main() {
      // Create the bytes to write to the temporary file.
      Byte[] bytesToWrite = new Byte[] { 1, 2, 3, 4, 5 };

      // Create the temporary file.
      FileStream fs = new FileStream("Temp.dat", FileMode.Create);

      // Write the bytes to the temporary file.
      fs.Write(bytesToWrite, 0, bytesToWrite.Length);

      // Explicitly close the file when done writing to it.
    //如果注释掉下面一行,则文件基本上不可能删除。
    ((IDisposable) fs).Dispose();

      // Delete the temporary file.
      File.Delete("Temp.dat");  // This always works now
   }
#endif


#if Version2
 public static void Main()
 {
  // Create the bytes to write to the temporary file.
  Byte[] bytesToWrite = new Byte[] { 1, 2, 3, 4, 5 };

  // Create the temporary file.
  FileStream fs = new FileStream("Temp.dat", FileMode.Create);
  try
  {
   // Write the bytes to the temporary file.
   fs.Write(bytesToWrite, 0, bytesToWrite.Length);
  }
  finally
  {
   // Explicitly close the file when done writing to it.
   if (fs != null)
    ((IDisposable)fs).Dispose();//或者fs.Close();
  }

  // Delete the temporary file.
  File.Delete("Temp.dat");  // This always works now.
 }
#endif


}


友情链接

郑州大学软件学院 SpringWidgets-Blogger 徵信社 翻译公司 链接帮手网 行驶证字体 酷站目录 Friend Connectified