Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, October 31, 2006 6:15 PM | 1 vote
Hey Everyone
I am writting an application where I want to create a file in memory, that may not be the right term, and then send in via an email to several people. By "in memory" I mean that I dont actually want to have the program create a file that would be on the hard drive. What I want is the file to be temporary but be able to be emailed. Can I do this? and if so how?
Thanks
Kenzie
All replies (5)
Tuesday, October 31, 2006 6:44 PM âś…Answered | 1 vote
You can use static method System.IO.Path.GetTempFileName() which will create a file with unique name in Tempory files folder in Windows and return full path of it, You can use that path to instantiate a FileStream object write to it use it, and it'll be deleted when some one will run Disk Clean up or if there is another routine for that. So you dont need to worry about that many programs use this place to store temp files!
I hope this will Help!
Best Regards,
Edit: this is a quick sample code: http://www.knowdotnet.com/articles/tempfiles.html
Best regards,
Tuesday, October 31, 2006 6:31 PM
you could use the MemoryStream (storage in bytes[] directly and retrieving using the GetBuffer() which returns bytes[]) to store your data
something what you maybe after?
Forgot something I did a while ago I think...but you would then need a way to reconstruct that file in memory so you can attach it via email.
could I ask why you wish to store it directly in memory? Why dont you delete the file once you have used it in the application? create it when it needs to be created then do your job then delete it once finished handling the file?
Tuesday, October 31, 2006 8:21 PM
The end program will be a Windows Service and I am not sure if I can create files in the application directory without messing up the service. If I could I would do it that way.
Kenzie
Tuesday, October 31, 2006 8:54 PM
interesting. im sure you can but don't quote me. I know this is maybe not quite what you are after however this code here gets the file contents in bytes[] and send the bytes[] as an attachment by email. Now wherever you are trying to creating the file, and send it as an attachment for example, can be done if you get the bytes[] of that file and place it into say, the memorystream. I hope this is useful for you in some way
FileStream theStream = new FileStream("somefile.ext", FileMode.Open); byte[] theFileBytes = new byte[theStream.Length]; theStream.Read(theFileBytes, 0, theFileBytes.Length); theStream.Close(); using (MemoryStream theMemStream = new MemoryStream()) { theMemStream.Write(theFileBytes, 0, theFileBytes.Length); System.Net.NetworkCredential theCred = new NetworkCredential("email@username", "Password"); System.Net.Mail.MailMessage theMsg = new System.Net.Mail.MailMessage("from@email", "to@email"); theMsg.Body = "hi"; theMsg.Subject = "test"; theMemStream.Position = 0; theMemStream.Seek(0, SeekOrigin.Begin); theMsg.Attachments.Add(new System.Net.Mail.Attachment(theMemStream, "")); System.Net.Mail.SmtpClient theClient = new System.Net.Mail.SmtpClient("smtp.ip.address.here"); theClient.Credentials = theCred; theClient.Send(theMsg); } |
of course, you just give it the path and location of file if you were sending it by email directly instead of reading it into the stream etc....
im not sure you can make a file in memory, different file formats etc...
Tuesday, October 31, 2006 10:57 PM
You can use a MemoryStream or a StringStream, both of which act exactly like FileStream, but keep their data strict in memory. When it comes time to create the email, you can attach a Stream (of any form) to the message.