How to add a stringformat in app.config

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Spippo
    New Member
    • Nov 2007
    • 15

    How to add a stringformat in app.config

    Hi,

    I want to create a string with string.format, but the format must be editable.
    Therefor i want to add the format in app.config and read it from the program and enter the correct values in it.

    This is my app.config-file:
    Code:
    <?xml version="1.0" encoding="utf-16" ?>
    <configuration>
      <appSettings>
        <!-- SimpleMailAddin settings -->
             <!-- MailSender, SMTPServer and MailBodyFormat are required -->
        <add key ="MailSender" value ="me@mydomain.com"/>
        <add key ="SMTPServer" value ="VMEXCH.mydomain.com"/>
             <!-- This is the foSystermatstring of the body of the message. Usable values : {0} = Event_ID  {1} = Event_Name  {2} = Event_StartDate  {3} = Event_EndDate  {4} = Event_Description -->
        <add key="MailBodyFormat" value="Event Reminder\n\nEvent       : {1} ({0})\nStartdate   : {2}\nEnddate     : {3}\n\n{4}"/>    
      </appSettings>
    </configuration>
    My code tries to fill in the string like this:
    Code:
    string body = System.Configuration.ConfigurationManager.AppSettings["MailBodyFormat"];
    body = string.format(body, Event_ID, Event_Name, Event_StartDate, Event_EndDate, Event_Description);
    If I run this code, my string "body" still is "Event Reminder\n\Even t : {1} ...." and not {Event Reminder\n\nEve nt : My event ..."

    Any way to solve this?
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    Is it giving any error?
    I did something like this it works fine

    <appSettings>
    <add key="MyConnecti onString" value="Driver={ SQL Server};Server= myServerAddress ;Database=myDat aBase;Uid={0};P wd={1};"/>
    </appSettings>

    string myConnectionStr ing = string.Empty;

    myConnectionStr ing = ConfigurationMa nager.AppSettin gs["MyConnectionSt ring"];

    myConnectionStr ing = string.Format(m yConnectionStri ng, myUserId, myPassword);

    Comment

    • Spippo
      New Member
      • Nov 2007
      • 15

      #3
      Hi,

      Thanks for the quick reply.
      It did work correctly, I made a mistake in my program.

      I did have a problem with the newline '\n', this was just display as the text "\n" and not as a newline.
      I solved this by replacing '\n' with {5} and set the sixth parameter as "\n".

      Thanks

      Comment

      Working...