Csharp/CSharp Tutorial/File Directory Stream/Using Statement

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create object inside the using statement

using System; 
using System.IO; 
 
class MainClass { 
  public static void Main() { 
     
    using(StreamReader sr2 = new StreamReader("test.txt")) { 
      Console.WriteLine(sr2.ReadLine()); 
      sr2.Close(); 
    } 
 
  } 
}
124.23

Dispose a StreamWriter by using "using"

using System;
using System.IO;
public sealed class MainClass
{
    static void Main(){
        using( StreamWriter sw = new StreamWriter("Output.txt") ) {
            sw.WriteLine( "This is a test of the emergency dispose mechanism" );
        }
    }
}

Nested using statement

using System;
using System.IO;
class MainClass
{
   static void Main()
   {
      using (TextWriter tw1 = File.CreateText("test.txt"))
      {
         tw1.WriteLine("test");
         using (TextWriter tw2 = File.CreateText("test2.txt"))
            tw2.WriteLine("test1");    
      }
      using (TextReader tr = File.OpenText("test.txt"))
      {
         string str;
         while (null != (str = tr.ReadLine()))
            Console.WriteLine(str);
         using(TextReader tr2 = File.OpenText("test2.txt"))
            while(null != (str = tr2.ReadLine()))
               Console.WriteLine(str);
      }
   }
}
test
test1

Use FileStream inside a using statement

using System;
using System.IO;
using System.Text;
public class MainClass
{
    static void Main() {
        using( FileStream fs = File.Open( "log.txt", FileMode.Append, FileAccess.Write, FileShare.None ) ) {
            Byte[] msg = new UTF8Encoding(true).GetBytes("string \n");
            fs.Write( msg, 0, msg.Length );
        }
    }
}

Use object inside using statement

using System; 
using System.IO; 
 
class MainClass { 
  public static void Main() { 
    StreamReader sr = new StreamReader("test.txt"); 
 
    using(sr) { 
      Console.WriteLine(sr.ReadLine()); 
      sr.Close(); 
    } 
 
  } 
}
124.23

Using statement to deal with multiple resources

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;             
class MainClass
{
   static void Main()
   {
      using (TextWriter tw1 = File.CreateText("test.txt"),
                        tw2 = File.CreateText("test2.txt"))
      {
         tw1.WriteLine("test");
         tw2.WriteLine("test1");
      }
      using (TextReader tr1 = File.OpenText("test.txt"),
                        tr2 = File.OpenText("test2.txt"))
      {
         string str;
         while (null != (str = tr1.ReadLine()))
            Console.WriteLine(str);
            
         while (null != (str = tr2.ReadLine()))
            Console.WriteLine(str);
      }
   }
}
test
test1

Using Statement: using DIRECTIVE, not using statement

using System;              
using System.IO; //using DIRECTIVE, not using statement
class MainClass
{
   static void Main()
   {
      using (TextWriter tw = File.CreateText("test.txt"))// using statement 
      {
         tw.WriteLine("string");
      }
      using (TextReader tr = File.OpenText("test.txt"))
      {
         string str;
         while (null != (str = tr.ReadLine()))
            Console.WriteLine(str);
      }
   }
}
string