protected void Button1_Click(object sender, EventArgs e) { //创建文件 //方法一:利用FileStream类和StreamWriter类创建 //string path = @"c:\test.html"; //FileStream fs = new FileStream(path, FileMode.Create); //StreamWriter sw = new StreamWriter(fs,Encoding.GetEncoding("GB2312")); //sw.Write("Hi,你好!<br/>"); //sw.Write("javaeye,你好!"); //sw.Close();//关闭流 //sw.Dispose();//释放资源 //方法二:利用File类创建并使用AppendAllText方法 //string path1 = @"c:\test2.txt"; //File.AppendAllText(path1, "欢迎来到陶院!"); //sw.Close(); //sw.Dispose(); //方法三:使用数组传入,使用File类 //string path1 = @"c:\test3.txt"; //string []content={"明天上课","晚上见面!"}; //File.WriteAllLines(path1, content); //sw.Close(); //sw.Dispose(); //方法四:利用利用FileInfo类和StreamWriter类创建 //string path2 = @"c:\test3.doc"; //FileInfo fl = new FileInfo(path2); //using (StreamWriter sw = fl.CreateText()) //{ // sw.WriteLine("今年是2011年"); // sw.WriteLine("元旦快乐!"); //} //sw.Close(); //sw.Dispose(); //读取文件 //方法一:利用StreamReader类和ReadLine方法读取 //string path2 = @"c:\test2.doc"; //StreamReader sr = new StreamReader(path2); //string con= sr.ReadLine(); //string content = sr.ReadToEnd(); //Response.Write(con+"<br/>"); //Response.Write(content); //方法二:利用File类和ReadAllLines方法传给数组变量conn //string path2 = @"c:\test2.doc"; //string []conn=File.ReadAllLines(path2, Encoding.UTF8); //foreach (string str in conn) //{ // Response.Write(str+"<br/>"); //} //方法三:利用FileStream类读取 //string path2 = @"c:\test2.doc"; //FileStream fs = new FileStream(path2, FileMode.Open); //byte[] array = new byte[30]; //fs.Read(array, 0, 30); //string con = Encoding.UTF8.GetString(array); //Response.Write(con); }