Qasim Iqbal Qasim Iqbal - 4 months ago 12
C# Question

How can i programmatically delete email attachments after I save them on my hard drive

So I am making an outlook add-in where I the program reads emails attachments and saves them in the windows:(c) directory. But i want the program to delete email attachments from the directory after program finish's reading the attachments.

Code:

string path = @"C:\\att\\" + attachment.FileName;
attachment.SaveAsFIle(Path);

Answer

To delete a file: System.IO.File.Delete(path)

https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx

You could iterate through the files inside the folder

https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx

string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
{
        System.IO.File.Delete(fileName);
}