读取和写入文件的最简单方法 [英] Easiest way to read from and write to files

查看:40
本文介绍了读取和写入文件的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中有很多不同的方式来读取和写入文件(文本文件,而不是二进制文件).

There are a lot of different ways to read and write files (text files, not binary) in C#.

我只需要简单且使用最少代码的东西,因为我将在我的项目中大量处理文件.我只需要 string 的东西,因为我只需要读写 strings.

I just need something that is easy and uses the least amount of code, because I am going to be working with files a lot in my project. I only need something for string since all I need is to read and write strings.

推荐答案

使用 File.ReadAllTextFile.WriteAllText.

MSDN 示例摘录:

// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);

...

// Open the file to read from.
string readText = File.ReadAllText(path);

这篇关于读取和写入文件的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆