使用C#在Windows应用程序中写入和读取txt文件 [英] Writing and reading from a txt file in windows application using c#

查看:84
本文介绍了使用C#在Windows应用程序中写入和读取txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,请在C#Windows应用程序中创建游戏,该游戏的目的是将所有按钮都设为红色,因此,如果所有按钮均为红色,则会显示一个表格,询问玩家的姓名,因此当在游戏的主界面上单击另一个被称为最高得分的玩家时,应该显示玩家输入的名称.我所知道的是必须创建一个txt文件并将其存储在名称上,然后才能显示该名称在另一种形式上,它将取自同一文件,但我不知道该如何处理..

解决方案

最简单的方法(不是最有效的方法,但当然最适合初学者)将是

 字符串 []行= File.ReadAllLines( @"  F:\ Temp \ NamesFile.txt"); 



 File.WriteAllLines( @" ,行); 

您所要做的就是修改两个语句之间的lines数组!


您将要使用TextReader和TextWriter类.

MSDN [ ^ ]

为什么不在互联网上进行搜索.
我有一些例子-
http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx [ ^ ]
http://support.microsoft.com/kb/816149 [ http://www.java2s.com/Code/CSharp/File-Stream/ReadandWriteaTextFile.htm [^ ]
http://bytes.com/topic/net/insights/673819-write- read-text-file-c [ ^ ]


hey guys please help,am creating a game in C# windows application the aim of the game is to make all the buttons red so if all the buttons are red a form is displayed asking for the player''s name,so the name entered by the player should be displayed when the other called top score is clicked on the main interface of the game.all i know is that a txt file has to be created and the name will be stored on it then for the name to displayed on another form it will be taken from the same file but i dont know how to go about that..

解决方案

The simplest way (not the most efficient, but certainly the easiest for a beginner) would be

string[] lines = File.ReadAllLines(@"F:\Temp\NamesFile.txt");


and

File.WriteAllLines(@"F:\Temp\NamesFile.txt", lines);

All you have to do is modify the lines array between the two statements!


You''ll want to use the classes TextReader and TextWriter.

There is an example here at MSDN[^]


Why don''t you do a search on the internet.
I got some examples -
http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx[^]
http://support.microsoft.com/kb/816149[^]
http://www.java2s.com/Code/CSharp/File-Stream/ReadandWriteaTextFile.htm[^]
http://bytes.com/topic/net/insights/673819-write-read-text-file-c[^]


这篇关于使用C#在Windows应用程序中写入和读取txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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