woodlawn_la
woodlawn_la

Reputation: 15

ASP.NET String Str Functions

!--
  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);
        }
    }
}

Upvotes: 0

Views: 485

Answers (1)

amiry jd
amiry jd

Reputation: 27585

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);
        }
    }
}

Upvotes: 1

Related Questions