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

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

问题描述

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

There are a lot of different ways to read and write files (Text Files not Binary) in C#.

我只需要一些简单,使用最少量的code,因为我将要使用的文件在我的项目的工作很多。我只需要一些弦乐,因为我需要的是读取和写入字符串。

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 Strings since all I need is to read and write Strings.

推荐答案

<一个href="http://msdn.microsoft.com/en-us/library/system.io.file.readalltext.aspx">File.ReadAllText

<一个href="http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx">File.WriteAllText

简单得不能再简单...

Could not be simpler...

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天全站免登陆