Question Attaching a file that doesn't exist

VentureFree

Well-known member
Joined
Jan 9, 2008
Messages
54
Programming Experience
5-10
I've got a logging system that emails me log files whenever certain conditions are met. Unfortunately I find myself in a situation where I am not allowed to write files to the disk that my program is running on. For the time being I'm just saving the logs internally as a big a-- String instead of writing them to a file and then just dumping the logs into the body of the email, which I think is ugly.

Is there a way to "create" a file that I can attach to the email without actually creating the file on the disk?
 
There are a few Attachment constructors that accept a Stream as content, so you can for example add the text to a MemoryStream and supply that. Example:
VB.NET:
Dim mem As New IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes("This is a test."))
Dim att As New Mail.Attachment(mem, "memoryfile.txt")
Attachment Constructor (System.Net.Mail)
 
Jeez, I feel kind of foolish. Both of my last questions had very simple answers that I could have easily found myself with even a cursory look at the documentation. The MSDN library will now be the first link I try when I have a question.

Thanks for your clear and concise answer, and for acting as if it wasn't a dumb question.
 
as if it wasn't a dumb question
Not at all, .Net Framework is very large and at times complex, and it depends on developers experience to be able to find and utilize information and put the pieces together. The 'answer' may be simple in the end, but getting there may not always be so.
 
Back
Top