Reputation: 46
In my solution I have a VB class library project. This class library has a folder with several html files which are supposed to be email templates. This class library is intended to be included with both a website and a console app to generate customer emails.
What I want to do is read these html templates into a string and replace keywords in the templates with the data from a simple data structure. At present I'm using a dictionary with the key as the keyword and the value as the string to replace it with.
The problem that I am having is that VB doesn't seem to want to find my html files.
Here's the code for my base email class
Imports System.Net.Mail
Imports System.IO
Public MustInherit Class Email
Public Property TheMailMessage As MailMessage
Protected MustOverride Property SendFrom As MailAddress
Protected MessageTemplate As StreamReader
Protected DataModel As Dictionary(Of String, String)
Protected BodyContent As String
Protected Function GenerateMessageBody() As String
BodyContent = MessageTemplate.ReadToEnd
For Each d In DataModel
BodyContent.Replace(d.Key, d.Value)
Next
Return BodyContent
End Function
Protected MustOverride Sub PopulateMailMessage()
Protected MustOverride Sub CreateDataModel()
End Class
Here's the code for the the class inheriting a child of Email that is trying to read the HTML file to for generating the message body content (I didn't include the call between because all it does is set up the from address):
Imports System.IO
Imports System.Net.Mail
Namespace CustomerEmails
Public Class Welcome : Inherits NoReply
Sub New(ByVal Client As NinjaNexus.Model.Client)
MyBase.New(Client)
MessageTemplate = New StreamReader("Welcome.html")
CreateDataModel(Client)
PopulateMailMessage()
End Sub
Protected Overrides Sub CreateDataModel()
Throw New NotImplementedException
End Sub
Protected Overrides Sub PopulateMailMessage()
TheMailMessage.Subject = "Welcome to Company Name"
TheMailMessage.Body = GenerateMessageBody()
End Sub
Protected Overloads Sub CreateDataModel(ByVal Client As NinjaNexus.Model.Client)
DataModel = New Dictionary(Of String, String)
DataModel.Add("{FName}", Client.Name)
DataModel.Add("{Signature}", "Some name here")
End Sub
End Class
End Namespace
When I try and run the code to generate the welcome email I get an error like this:
An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll but was not handled in user code
Additional information: Could not find file 'C:\Program Files (x86)\IIS Express\Welcome.html'.
I've tried a few things like GetFullPath and the like, but that hasn't worked. Adding the folder name or ~\ or .\ or anything of that nature does not help. If I use the complete, full absolute path it reads the file. However, this path isn't going to be the same on the machines running the finished applications, so I really need a relative solution.
Does anyone know how to get the StreamReader to read my HTML file correctly? Is there a better approach than using a StreamReader? I want to stress that this library is going to be used for multiple related projects, so ideally I want to keep all the resources it needs with it and not hanging out on some file server somewhere.
Upvotes: 1
Views: 133
Reputation: 46
It turns out the answer is to set the build action for the files with my templates to "embedded resource." From there I can then use GetManifestResourceStream to get the contents of the file and do what I wish. I also switched the HTML files to TXT files. Though I still feel like there might be a better way to accomplish my goal, this works.
Upvotes: 1
Reputation: 335
I believe you want to look for file in your local computer. You can check here...
You may want to use AppDat folder to store your AppData (Recommended). Check out the snippet below:
Imports System.Environment
Class Sample
Public Shared Sub Main()
' Get the path to the Application Data folder
Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData)
' Display the path
Console.WriteLine("App Data Folder Path: " & appData)
End Sub
End Class
Upvotes: 0