C# ASP.NET字符串Str函数
在您的代码中:C# ASP.NET字符串Str函数,c#,asp.net,C#,Asp.net,在您的代码中: var str = WebConfigurationManager.AppSettings["REPLACE_TEXT"]; // = themessage = %1 str = string.Format("'{0}'", str); // = 'themessage = %1' return str; // and use it 这里缺少一些上下文。这段代码是什么xml文件的一部分?你应该发布实际执行替换的代码。为了澄清要点,我更新了这篇文章。这不是一个字符串函数。您只是在
var str = WebConfigurationManager.AppSettings["REPLACE_TEXT"]; // = themessage = %1
str = string.Format("'{0}'", str); // = 'themessage = %1'
return str; // and use it
这里缺少一些上下文。这段代码是什么xml文件的一部分?你应该发布实际执行替换的代码。为了澄清要点,我更新了这篇文章。这不是一个字符串函数。您只是在一个XML文件中转义引号,该文件恰好是您的.config文件。尝试使用
'代码>当在双引号内时。换句话说,此代码:是否可以正常工作?您能否指导我如何将您提到的代码放入default.aspx.cs文件中?谢谢。我在上面列出了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);
}
}
}
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);
}
}
}