【问题标题】:Get string values from txt file in App_Code folder从 App_Code 文件夹中的 txt 文件中获取字符串值
【发布时间】:2012-02-09 09:02:44
【问题描述】:

在我的 Web 应用程序中,我有一个 txt 文件,其中包含多个字符串值(形成我的表单的标签)。 如何从该文件中获取这些值,例如:

Label1.Text = 从 constant.txt 文件中获取字符串 Label1

有没有可能,或者最好遵循另一种方式?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我认为你最好使用资源 http://msdn.microsoft.com/en-us/library/ms227427.aspx

    创建资源后,您可以通过这种方式简单地使用它们

    <asp:Button ID="Button1" runat="server" 
    Text="<%$ Resources:WebResources, Button1Caption %>" />
    

    很简单

    【讨论】:

    • 谢谢你,Marcuz,我会尝试使用资源。路易吉
    【解决方案2】:

    试试这个代码,你可以通过两种方式读取 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();
    

    希望得到它的帮助。

    【讨论】:

    • 这个解决方案的问题是我每次只能得到一个值,把它作为我标签的标题。路易吉
    • 我已经编辑了我的代码。首先尝试一下。您可以获得带有值的字符串集合..而不是根据您的要求在代码中使用。
    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多