如何从txt文件读取文本并将其保存在字符串变量中 [英] How to read text from txt file and save it in string variable
本文介绍了如何从txt文件读取文本并将其保存在字符串变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友们,我正在使用C#开发一个Windows应用程序.
我想从文件中读取内容并将其存储在变量中.
内容将作为其他文件的路径.
如何阅读
谢谢
HI friends i am developing one windows application using C# .
i want to read the content from file and store it in a variable.
the content will be as a path of some other file .
how to read it
thank you
推荐答案
您可以使用:
You can use:
string contents = File.ReadAllText( filepath );
尼克
Nick
在页面顶部,您需要:
At the top of your page you require:
<br />
using System.IO;<br />
然后在哪里进行读取,您将需要:
Then where you are doing the read you''ll require:
<br />
FileStream file = new FileStream("c:\path\to\file.txt", FileMode, FileAccess);<br />
<br />
StreamReader sr = new StreamReader(file);<br />
<br />
string s = sr.ReadToEnd();<br />
<br />
sr.Close();<br />
<br />
file.Close();<br />
<br />
希望有帮助!
Hope that helps!
这篇关于如何从txt文件读取文本并将其保存在字符串变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文