C#提供的类库能够轻松实现对文件的操作
生活随笔
收集整理的這篇文章主要介紹了
C#提供的类库能够轻松实现对文件的操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//C#寫入/讀出文本文件
string fileName =@"c:I.txt";
StreamReader sr = new StreamReader(fileName); string str=sr.ReadLine (); sr.close();
StreamWriterrw=File.CreateText(Server.MapPath(".")+"\myText.txt");
rw.WriteLine("寫入");
rw.WriteLine("abc");
rw.WriteLine(".NET筆記");
rw.Flush();
rw.Close();
//打開文本文件
StreamReadersr=File.OpenText(Server.MapPath(".")+"\myText.txt");
StringBuilderoutput=newStringBuilder();
stringrl;
while((rl=sr.ReadLine())!=null)
...{
output.Append(rl+"");
}
lblFile.Text=output.ToString();
sr.Close();
//C#追加文件
StreamWritersw=File.AppendText(Server.MapPath(".")+"\myText.txt");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET筆記");
sw.Flush();
sw.Close();
//C#拷貝文件
stringOrignFile,NewFile;
OrignFile=Server.MapPath(".")+"\myText.txt";
NewFile=Server.MapPath(".")+"\myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);
//C#刪除文件
stringdelFile=Server.MapPath(".")+"\myTextCopy.txt";
File.Delete(delFile);
//C#移動文件
stringOrignFile,NewFile;
OrignFile=Server.MapPath(".")+"\myText.txt";
NewFile=Server.MapPath(".")+"\myTextCopy.txt";
File.Move(OrignFile,NewFile);
//C#創(chuàng)建目錄
//創(chuàng)建目錄c:sixAge
DirectoryInfod=Directory.CreateDirectory("c:\sixAge");
//d1指向c:sixAgesixAge1
DirectoryInfod1=d.CreateSubdirectory("sixAge1");
//d2指向c:sixAgesixAge1sixAge1_1
DirectoryInfod2=d1.CreateSubdirectory("sixAge1_1");
//將當(dāng)前目錄設(shè)為c:sixAge
Directory.SetCurrentDirectory("c:\sixAge");
//創(chuàng)建目錄c:sixAgesixAge2
Directory.CreateDirectory("sixAge2");
//創(chuàng)建目錄c:sixAgesixAge2sixAge2_1
Directory.CreateDirectory("sixAge2\sixAge2_1");
string fileName =@"c:I.txt";
StreamReader sr = new StreamReader(fileName); string str=sr.ReadLine (); sr.close();
StreamWriterrw=File.CreateText(Server.MapPath(".")+"\myText.txt");
rw.WriteLine("寫入");
rw.WriteLine("abc");
rw.WriteLine(".NET筆記");
rw.Flush();
rw.Close();
//打開文本文件
StreamReadersr=File.OpenText(Server.MapPath(".")+"\myText.txt");
StringBuilderoutput=newStringBuilder();
stringrl;
while((rl=sr.ReadLine())!=null)
...{
output.Append(rl+"");
}
lblFile.Text=output.ToString();
sr.Close();
//C#追加文件
StreamWritersw=File.AppendText(Server.MapPath(".")+"\myText.txt");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET筆記");
sw.Flush();
sw.Close();
//C#拷貝文件
stringOrignFile,NewFile;
OrignFile=Server.MapPath(".")+"\myText.txt";
NewFile=Server.MapPath(".")+"\myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);
//C#刪除文件
stringdelFile=Server.MapPath(".")+"\myTextCopy.txt";
File.Delete(delFile);
//C#移動文件
stringOrignFile,NewFile;
OrignFile=Server.MapPath(".")+"\myText.txt";
NewFile=Server.MapPath(".")+"\myTextCopy.txt";
File.Move(OrignFile,NewFile);
//C#創(chuàng)建目錄
//創(chuàng)建目錄c:sixAge
DirectoryInfod=Directory.CreateDirectory("c:\sixAge");
//d1指向c:sixAgesixAge1
DirectoryInfod1=d.CreateSubdirectory("sixAge1");
//d2指向c:sixAgesixAge1sixAge1_1
DirectoryInfod2=d1.CreateSubdirectory("sixAge1_1");
//將當(dāng)前目錄設(shè)為c:sixAge
Directory.SetCurrentDirectory("c:\sixAge");
//創(chuàng)建目錄c:sixAgesixAge2
Directory.CreateDirectory("sixAge2");
//創(chuàng)建目錄c:sixAgesixAge2sixAge2_1
Directory.CreateDirectory("sixAge2\sixAge2_1");
? 但是,在對txt文件讀的操作中貌似沒問題。因?yàn)榇a能實(shí)現(xiàn)文件的讀操作,但是所讀txt文件包含中文的時(shí)候就以亂碼顯示。查了半天資料,看似復(fù)雜的問題其實(shí)很簡單就能解決,稍微改動一下即可:
StreamReader sr = new StreamReader(fileName,Encoding.GetEncoding("gb2312"));轉(zhuǎn)載于:https://www.cnblogs.com/yongbin621/archive/2009/07/03/1516101.html
總結(jié)
以上是生活随笔為你收集整理的C#提供的类库能够轻松实现对文件的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CAS证书分析(2)
- 下一篇: c# char unsigned_dll