和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));