麓谷官网欢迎你访问长沙北大青鸟麓谷校区,支持你成为一个受人尊重的专业人才!
当前位置: 首页 > 青鸟知识 > net

C#文件及目录操作类汇总

来源:长沙北大青鸟|发布时间:2017-03-27|浏览量:

学IT,好工作

就读长沙岳麓职业培训学校

求学热线: 400-160-2868

  C# 文件操作类大全

accp8.png

  1、读取文件

  1.1.操作系统默认编码

  //using

  System.IO;

  StreamReader s = File.OpenText(%%1);

  string %%2 = null;

  while ((%%2 = s.ReadLine()) != null){

  %%3

  }

  s.Close();

  1.2.UTF-8编码

  /*

  using

  System.IO;

  using System.Text;

  */

  StreamReader srfile = new

  StreamReader(%%1,Encoding.UTF8);

  while ((String %%2 = srfile.ReadLine()) !=

  null)

  {

  %%3

  }

  srfile.Close();

  1.3.分块读取

  /*

  using

  System.IO;

  using System.Text;

  */

  try

  {

  FileStream fs=new

  FileStream(%%1,FileMode.Open,FileAccess.Read);

  BinaryReader br=new

  BinaryReader(fs,new ASCIIEncoding());

  byte[]

  chunk;

  do

  {

  chunk=br.ReadBytes(10240);

  if(chunk.Length>0)

  {

  %%2

  //chunk,chunk.Length

  }

  }

  while(chunk.Length>0);

  fs.Close();

  }

  catch

  {

  //return

  -1;

  }

  2.写入文件

  //using System.IO;

  FileInfo f = new

  FileInfo(%%1);

  StreamWriter w =

  f.CreateText();

  w.WriteLine(%%2);

  w.Flush();

  w.Close();

  3.移动一个目录下所有文件到一个文件夹中

  /*

  using

  System.IO;

  using System.Collections;

  */

  string path =

  (%%2.LastIndexOf(Path.DirectorySeparatorChar) == %%2.Length - 1) ? %%2 :

  %%2+Path.DirectorySeparatorChar;

  string parent =

  Path.GetDirectoryName(%%1);

  Directory.CreateDirectory(path +

  Path.GetFileName(%%1));

  DirectoryInfo dir = new

  DirectoryInfo((%%1.LastIndexOf(Path.DirectorySeparatorChar) == %%1.Length - 1) ?

  %%1 : %%1 + Path.DirectorySeparatorChar);

  FileSystemInfo[] fileArr =

  dir.GetFileSystemInfos();

  Queue Folders = new

  Queue(dir.GetFileSystemInfos());

  while

  (Folders.Count>0)

  {

  FileSystemInfo tmp = Folders.Dequeue();

  FileInfo

  f = tmp as FileInfo;

  if (f == null)

  {

  DirectoryInfo d = tmp as

  DirectoryInfo;

  foreach (FileSystemInfo fi in

  d.GetFileSystemInfos())

  {

  Folders.Enqueue(fi);

  }

  }

  else

  {

  f.MoveTo(path+f.Name);

  }

  }

  4.文件RSA高级加密

  /*

  using

  System.IO;

  using System.Security.Cryptography;

  private

  RSACryptoServiceProvider _rsa;

  */

  FileStream fin=new

  FileStream(%%1,FileMode.Open,FileAccess.Read);

  FileStream fout=new

  FileStream(%%2,FileMode.OpenOrCreate,FileAccess.Write);

  byte[] readBuffer=new

  byte[128];

  fin.Read(readBuffer,0,readBuffer.Length);

  byte[]

  encryptedBuffer=_rsa.Encrypt(readBuffer,true);

  _rsa.Clear();

  5.计算文件大小

  /*

  using

  System.IO;

  private const long KB=1024;

  private const long

  MB=1024*KB;

  private const long GB=1024*MB;

  */

  FileInfo fi = new

  FileInfo(%%1);

  long filesize= fi.Length;

  string

  showsize;

  if(filesize>=GB)

  showsize.format("%0.2f

  GB",(double)filesize/GB);

  else if(filesize>=MB)

  showsize.format("%0.2f

  MB",(double)filesize/MB);

  else if(filesize>=KB)

  showsize.format("%0.2f

  KB",(double)filesize/KB);

  else if(filesize>1)

  showsize.format("%ld

  Bytes",filesize);

  else

  showsize="1 Byte";

  string

  %%2=showsize;

  6.计算文件夹的大小

  /*

  using System.IO;

  private const

  long KB=1024;

  private const long MB=1024*KB;

  private const long

  GB=1024*MB;

  */

  private static long FolderFileSize(string

  path)

  {

  long size = 0;

  try

  {

  FileInfo [] files = (new

  DirectoryInfo(path)).GetFiles();

  foreach(FileInfo file in files)

  {

  size

  += file.Length;

       }

  }

  catch(Exception

  ex)

  {

  MessageBox.Show(ex.Message);

  }

  return

  size;

  }

  private static long FolderSize(string path)

  {

  long Fsize

  = 0;

  try

  {

  Fsize = FolderFileSize(path);

  DirectoryInfo [] folders =

  (new DirectoryInfo(path)).GetDirectories();

  foreach(DirectoryInfo folder in

  folders)

  Fsize += FolderSize(folder.FullName);

  }

  catch(Exception

  ex)

  {

  MessageBox.Show(ex.Message);

  }

  return Fsize;

  }

  long

  filesize= FolderSize(%%1);

  string

  showsize;

  if(filesize>=GB)

  showsize.format("%0.2f

  GB",(double)filesize/GB);

  else if(filesize>=MB)

  showsize.format("%0.2f

  MB",(double)filesize/MB);

  else if(filesize>=KB)

  showsize.format("%0.2f

  KB",(double)filesize/KB);

  else if(filesize>1)

  showsize.format("%ld

  Bytes",filesize);

  else

  showsize="1 Byte";

  string

  %%2=showsize;

  7.快速获得当前程序的驱动器、路径、文件名和扩展名

  //using

  System.IO;

  DirectoryInfo dires= new

  DirectoryInfo(Application.StartupPath);

  string strQDQ = null;//驱动器

  strQDQ

  = dires.Root.ToString();

  string strPath

  =Application.ExecutablePath;//路径

  string FName = null;//文件名

  string

  FNameExt = null;//扩展名

  FileInfo FileIno = new FileInfo(strPath);

  FName =

  FileIno.Name;

  FNameExt =

  FileIno.Extension;

上一篇:ASP.Net开发:WebForm和MVC的区别

下一篇:如何用C#创建和读取XML文件

扫码关注微信公众号了解更多详情

跟技术大咖,专业导师一起交流学习

姓名
电话
Q Q

在线留言

请您把问题留下,我们为您提供专业化的解答!

QQ咨询
  1. 招生问答
  2. 热门点击
  3. 最新更新
  4. 推荐文章

关于我们

学校成就

就业保障

联系方式

联系电话:400-160-2868

在线报名

预约报名

备案号:湘ICP备2020021619号-1
地址:湖南省长沙市高新区麓谷麓松路679号 版权所有:长沙市岳麓职业培训学校

在线咨询
课程咨询 学费咨询 学费分期 入学测试 免费预约 来校路线
初中生 高中生 待业者
400-160-2868

在线客服