日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#提供的类库能够轻松实现对文件的操作

發(fā)布時(shí)間:2023/12/10 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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");

? 但是,在對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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。