0
!--
  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>

Within the app settings section there is a value="themessage = %1" I need for the value to interpret the string as 'themessage = %1' instead of themessage = %1

What do I need to enclose the 'themessage = %1' with in order to do this? I have tried "'themessage = %1'" and "/'themessage = %1'/" and neither works.

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

This is the Default.aspx.cs file.

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);
        }
    }
}
5
  • 2
    There's some context missing here. What xml file is this code part of? Commented Mar 12, 2014 at 2:44
  • You should post the code that actually does the replace. Commented Mar 12, 2014 at 4:42
  • I have update the post in order to clarify points. Commented Mar 12, 2014 at 20:41
  • This is not a string function. You're just escapaing quotes in an XML file, that happens to be your .config file. Try using &#39; when inside the double quotes. Commented Mar 12, 2014 at 20:43
  • In other words this code: <add key="REPLACE_TEXT" value="&#39themessage = %1&#39" /> would function as <add key="REPLACE_TEXT" value="'themessage = %1'" /> am I correct? Commented Mar 12, 2014 at 23:59

1 Answer 1

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

IN YOUR CODE:

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Could you guide me in how to place the code you mentioned into the default.aspx.cs file? Thanks.
I listed the default.aspx.cs file above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.