!--
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>
在应用设置部分中有一个值=“themessage =%1” 我需要将值解释为'themessage =%1'而不是themessage =%1
为了做到这一点,我需要将'themessage =%1'括起来? 我试过“'themessage =%1'”和“/'themessage =%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);
}
}
}
答案 0 :(得分:1)
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);
}
}
}