ASP.NET String Str Functions

本文关键字:Functions Str String NET ASP | 更新日期: 2023-09-27 17:49:50

!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <appSettings>
    <add key="BATCH_FILE_LOCATION" value="C:'inetpub'webpublish'batchfile.bat"/>
    <add key="REPLACE_TEXT" value="themessage = %1" />
  </appSettings>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.'SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|'aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
  </system.web>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

在应用程序设置部分有一个值=" themmessage = %1"我需要值将字符串解释为' themmessage = %1'而不是themmessage = %1

我需要在' themmessage = %1'中加上什么才能做到这一点?我已经尝试了"' themmessage = %1'"answers"/' themmessage = %1'/",但都不起作用。

=============================================================================

这是Default.aspx.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Configuration;
namespace JamesInputProject
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string batchFilePath=WebConfigurationManager.AppSettings["BATCH_FILE_LOCATION"];
           string batText= File.ReadAllText(batchFilePath);
           string batReplacedText=batText.Replace(WebConfigurationManager.AppSettings["REPLACE_TEXT"], txtInput.Text);
           //var str = WebConfigurationManager.AppSettings["REPLACE_TEXT"]; // = themessage = %1
           //str = string.Format("'{0}'", str); // = 'themessage = %1'
            //if you dont want to override existing batch file use this
            string outputFilePath = batchFilePath.Remove(batchFilePath.Length - 4) + "_" + DateTime.Now.ToString("ddMMyyyHHmm") + ".bat";
            //if you want to override existing batch file use this
            //string outputFilePath = batchFilePath;
            File.WriteAllText(outputFilePath, batReplacedText);
            lbMessage.Text = string.Format("The Batch File Input is replaced with {0} and written in the file {1}", txtInput.Text, outputFilePath);
        }
    }
}

ASP.NET String Str Functions

var str = WebConfigurationManager.AppSettings["REPLACE_TEXT"]; // = themessage = %1
str = string.Format("'{0}'", str); // = 'themessage = %1'
return str; // and use it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Configuration;
namespace JamesInputProject
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        // create a helper method, just to be more readable
        private string GetReplaceText() {
            var str = WebConfigurationManager.AppSettings["REPLACE_TEXT"]; // = themessage = %1
            str = string.Format("'{0}'", str); // = 'themessage = %1'
            return str; // and use it
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
           string batchFilePath=WebConfigurationManager.AppSettings["BATCH_FILE_LOCATION"];
           string batText= File.ReadAllText(batchFilePath);
           // and call that method:
           string batReplacedText = batText.Replace(GetReplaceText(), txtInput.Text);
           //var str = WebConfigurationManager.AppSettings["REPLACE_TEXT"]; // = themessage = %1
           //str = string.Format("'{0}'", str); // = 'themessage = %1'
            //if you dont want to override existing batch file use this
            string outputFilePath = batchFilePath.Remove(batchFilePath.Length - 4) + "_" + DateTime.Now.ToString("ddMMyyyHHmm") + ".bat";
            //if you want to override existing batch file use this
            //string outputFilePath = batchFilePath;
            File.WriteAllText(outputFilePath, batReplacedText);
            lbMessage.Text = string.Format("The Batch File Input is replaced with {0} and written in the file {1}", txtInput.Text, outputFilePath);
        }
    }
}