如何从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!