您现在的位置是:首页 > 文章详情

C#中的文件操作

日期:2018-08-01点击:409
原文: C#中的文件操作

读操作:

方法1:

string str = File.ReadAllText(@filepath, Encoding.Default);

方法2:

byte[] buffer = File.ReadAllBytes(@filepath); string str = Encoding.Default.GetString(buffer);

方法3:

string[] buffer = File.ReadAllLines(@filepath,Encoding.Default); string str = "" ; foreach (var buf in buffer) { str = str + buf +"\n"; }

方法4:

FileStream fsRead = new FileStream(@filepath, FileMode.OpenOrCreate); byte[] buffer = new byte[1024 * 1024 * 5]; int length = fsRead.Read(buffer, 0, buffer.Length); string str = Encoding.Default.GetString(buffer, 0, length); fsRead.Close(); fsRead.Dispose();

方法5:

string str =""; using (FileStream fsRead = new FileStream(@filepath, FileMode.OpenOrCreate,FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 5]; int length = 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 = new FileStream(@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 = new FileStream(@filepath, FileMode.OpenOrCreate, FileAccess.Write)) { fsWrite.Write(buffer, 0, buffer.Length); }

操作文件夹:

CreateDirectory:创建文件夹

Delete:删除文件夹

Move:剪切文件夹

Exist:判断是否存在

GetFiles:获得指定的目录下所有文件的全路径

GetDirectory:获得指定目录下所有文件夹的全路径

 

原文链接:https://yq.aliyun.com/articles/678138
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章