Eric Yih's Blog

Do what you like, like what you do.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Write the text to a file by using StreamWriter

Posted on 2008-12-14 00:01  Eric Yih  阅读(221)  评论(0)    收藏  举报
   1:  /*
   2:   * Created by SharpDevelop.
   3:   * User: Eric Yih
   4:   * Date: 2008-12-13
   5:   * Time: 20:05
   6:   * 
   7:   * To change this template use Tools | Options | Coding | Edit Standard Headers.
   8:   
*/
   
9:  using System;
  
10:  using System.IO;
  
11:  using System.Diagnostics;
  
12:   
  
13:  namespace Studying.MyStreamWriter
  
14:  {
  
15:      class MyStreamWriter
  
16:      {
  
17:          public void writeFile(string username, string password)
  
18:          {
  
19:              //Initialize StreamWriter as a Object "sw", and specify the file name and directory.
  20:              StreamWriter sw = new StreamWriter(System.Environment.CurrentDirectory + "\\userinfo.bat");
  
21:              sw.WriteLine(username);
  
22:              sw.WriteLine(password);
  
23:              //Close Stream
  24:              sw.Close();
  
25:          }
  
26:          
  
27:          public static void Main(string[] args)
  
28:          {
  
29:              MyStreamWriter myStreamWriter = new MyStreamWriter();
  
30:              myStreamWriter.writeFile("root","password");
  
31:          }
  
32:      }
  
33:  }

The result of this program:

 

root
password