读操作:
方法1:
stringstr = File.ReadAllText(@filepath, Encoding.Default);
方法2:
byte[] buffer =File.ReadAllBytes(@filepath);stringstr = Encoding.Default.GetString(buffer);
方法3:
string[] buffer =File.ReadAllLines(@filepath,Encoding.Default);stringstr ="";foreach(varbufinbuffer)
{
str= str + buf +"\n";
}
方法4:
FileStream fsRead =newFileStream(@filepath, FileMode.OpenOrCreate);byte[] buffer =newbyte[1024*1024*5];intlength = fsRead.Read(buffer,0, buffer.Length);stringstr = Encoding.Default.GetString(buffer,0, length);
fsRead.Close();
fsRead.Dispose();
方法5:
stringstr ="";using(FileStream fsRead =newFileStream(@filepath, FileMode.OpenOrCreate,Fileaccess.Read))
{byte[] buffer =newbyte[1024*1024*5];intlength = fsRead.Read(buffer,0, buffer.Length);
str= Encoding.Default.GetString(buffer,0, length);
}
写操作:
方法1:
File.WriteAllText(@filepath, str, Encoding.Default);
方法2:
byte[] buffer =Encoding.Default.GetBytes(str);
File.WriteAllBytes(@filepath,buffer);
方法3:
File.WriteAllLines(@filepath, str.Split(''),Encoding.Default);
方法4:
FileStream fsWrite =newFileStream(@filepath, FileMode.OpenOrCreate, FileAccess.Write);byte[] buffer =Encoding.Default.GetBytes(str);
fsWrite.Write(buffer,0, buffer.Length);
fsWrite.Close();
fsWrite.Dispose();
方法5:
byte[] buffer =Encoding.Default.GetBytes(str);using(FileStream fsWrite =newFileStream(@filepath, FileMode.OpenOrCreate, FileAccess.Write))
{
fsWrite.Write(buffer,0, buffer.Length);
}
操作文件夹:
CreateDirectory:创建文件夹
Delete:删除文件夹
Move:剪切文件夹
Exist:判断是否存在
GetFiles:获得指定的目录下所有文件的全路径
GetDirectory:获得指定目录下所有文件夹的全路径