C#文件操作

  • Post author:
  • Post category:其他





判断文件是否存在,不存在则创建;

if (!File.Exists(filename))

{

FileStream file = File.Create(filename);

file.Close();

}



文件尾添加数据;

using (FileStream filedata = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))

{

filedata.Seek(0, SeekOrigin.End);

filedata.Write(Data, 0, Data.Length);

filedata.Flush();

filedata.Close();

}

文件读取数据;

FileStream filedata = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);

public byte[] ReadFileData(FileStream file, int offset, int count)

{


byte[] buf = new byte[count];

file.Seek(offset, SeekOrigin.Begin);

file.Read(buf, 0, count);

file.Close();

return buf;

}

路径操作;



创建路径;

if (!Directory.Exists(path))

{


Directory.CreateDirectory(path);

}



遍历路径;

public void ShowDirFiles()

{


DirectoryInfo dir = new DirectoryInfo(path);

foreach (DirectoryInfo d in dir.GetDirectories())

{


Console.WriteLine(d.Name);

}

foreach (FileInfo f in dir.GetFiles(“*.txt”))

{



Console.WriteLine(f.Name);

f.Delete();

}

}



版权声明:本文为zcg1041原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。