和Java一樣C#提供的類庫能夠輕松實現對文件的操作下面就給出代碼示例大家可以參考一下
//C#寫入/讀出文本文件
string fileName =@c:Itxt;
StreamReader sr = new StreamReader(fileName); string str=srReadLine (); srclose();
StreamWriterrw=FileCreateText(ServerMapPath()+/myTexttxt);
rwWriteLine(寫入);
rwWriteLine(abc);
rwWriteLine(NET筆記);
rwFlush();
rwClose();
//打開文本文件
StreamReadersr=FileOpenText(ServerMapPath()+/myTexttxt);
StringBuilderoutput=newStringBuilder();
stringrl;
while((rl=srReadLine())!=null)
{
outputAppend(rl+);
}
lblFileText=outputToString();
srClose();
//C#追加文件
StreamWritersw=FileAppendText(ServerMapPath()+/myTexttxt);
swWriteLine(追逐理想);
swWriteLine(kzlll);
swWriteLine(NET筆記);
swFlush();
swClose();
//C#拷貝文件
stringOrignFileNewFile;
OrignFile=ServerMapPath()+/myTexttxt;
NewFile=ServerMapPath()+/myTextCopytxt;
FileCopy(OrignFileNewFiletrue);
//C#刪除文件
stringdelFile=ServerMapPath()+/myTextCopytxt;
FileDelete(delFile);
//C#移動文件
stringOrignFileNewFile;
OrignFile=ServerMapPath()+/myTexttxt;
NewFile=ServerMapPath()+/myTextCopytxt;
FileMove(OrignFileNewFile);
//C#創建目錄
//創建目錄c:sixAge
DirectoryInfod=DirectoryCreateDirectory(c:/sixAge);
//d指向c:sixAgesixAge
DirectoryInfod=dCreateSubdirectory(sixAge);
//d指向c:sixAgesixAgesixAge_
DirectoryInfod=dCreateSubdirectory(sixAge_);
//將當前目錄設為c:sixAge
DirectorySetCurrentDirectory(c:/sixAge);
//創建目錄c:sixAgesixAge
DirectoryCreateDirectory(sixAge);
//創建目錄c:sixAgesixAgesixAge_
DirectoryCreateDirectory(sixAge/sixAge_);
但是在對txt文件讀的操作中貌似沒問題因為代碼能實現文件的讀操作但是所讀txt文件包含中文的時候就以亂碼顯示查了半天資料看似復雜的問題其實很簡單就能解決稍微改動一下即可
StreamReader sr = new StreamReader(fileNameEncodingGetEncoding(gb));
From:http://tw.wingwit.com/Article/program/net/201311/13220.html