Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Can't I do String.Replace() on a IO.File.ReadAllText() string?

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?

like image 251
sholsinger Avatar asked Dec 13 '22 03:12

sholsinger


1 Answers

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);
like image 188
Dave Markle Avatar answered Dec 16 '22 16:12

Dave Markle