【发布时间】:2012-02-09 09:02:44
【问题描述】:
在我的 Web 应用程序中,我有一个 txt 文件,其中包含多个字符串值(形成我的表单的标签)。 如何从该文件中获取这些值,例如:
Label1.Text = 从 constant.txt 文件中获取字符串 Label1
有没有可能,或者最好遵循另一种方式?
【问题讨论】:
在我的 Web 应用程序中,我有一个 txt 文件,其中包含多个字符串值(形成我的表单的标签)。 如何从该文件中获取这些值,例如:
Label1.Text = 从 constant.txt 文件中获取字符串 Label1
有没有可能,或者最好遵循另一种方式?
【问题讨论】:
我认为你最好使用资源 http://msdn.microsoft.com/en-us/library/ms227427.aspx
创建资源后,您可以通过这种方式简单地使用它们
<asp:Button ID="Button1" runat="server"
Text="<%$ Resources:WebResources, Button1Caption %>" />
很简单
【讨论】:
试试这个代码,你可以通过两种方式读取 txt 文件。
(1路)
string value = File.ReadAllText(Server.MapPath("~/App_Code/t.txt"));
string[] ar={"\r\n"};
string[] split = value.Split(ar,StringSplitOptions.RemoveEmptyEntries);
(2路)
TextReader tr = new StreamReader(@"Path of appcode");
//Reading all the text of the file.
Console.WriteLine(trs.ReadToEnd());
//Or Can Reading a line of the text file.
Console.WriteLine(trs.ReadLine());
//Close the file.
trs.Close();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
希望得到它的帮助。
【讨论】: