views:

379

answers:

1

I am using System.IO.FIle.ReadAllText() to get the contents of some template files that I created for email content. Then I want to do a Replace on certain tokens within the files so I can add dynamic content to the template.

Here is the code I have, it seems to me like it should work just fine...

Dim confirmUrl As String = Request.ApplicationPath & "?v=" & reg.AuthKey
Dim text As String = IO.File.ReadAllText( _
   ConfigurationManager.AppSettings("sign_up_confirm_email_text").Replace("~", _
   Request.PhysicalApplicationPath))
Dim html As String = IO.File.ReadAllText( _
   ConfigurationManager.AppSettings("sign_up_confirm_email_html").Replace("~", _
   Request.PhysicalApplicationPath))

text.Replace("%%LINK%%", confirmUrl)
text.Replace("%%NAME%%", person.fname)

html.Replace("%%LINK%%", confirmUrl)
html.Replace("%%NAME%%", person.fname)

For some reason I cannot get the %%LINK%% and %%NAME%% Replace() calls to work properly. I checked to see if it was encoding-related, so I made each file UTF-8. And also used the forced encoding overload of ReadAllText(String, Encoding) and still no dice. Any ideas?

+12  A: 

The problem is that strings are immutable in .NET. So your Replace code should look like:

text = text.Replace("%%LINK%%", confirmUrl);
text = text.Replace("%%NAME%%", person.fname);

html = html.Replace("%%LINK%%", confirmUrl);
html = html.Replace("%%NAME%%", person.fname);
Dave Markle
They are immutable in VB as well.
Dour High Arch
edited accordingly.
Dave Markle
D'oh! That was silly of me. Thanks everyone.
sholsinger
Many would argue that having Replace() be a non-static function was silly of the .NET designers, so don't feel so bad ;-)
Dave Markle
Yeah, I think *every* .NET developer (and Java too, if I recall correctly) ran into this at some point. Even fully knowing of string immutability. Hell, I *still* forget to use the return value sometimes.
JulianR