Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im using one form in my application to send the mail..im attaching a pdf in the mail and sending the mail..when i send the the mail pdf file will be generated in a particular location..for example--
D:\ABC Report 06202012 42554 PM.pdf
...when i send the mail report has been generated all the time..how to delete that pdf using C# coding after sending the mail..when i use file.move,file.replace or file.delete it is showing an error
The process cannot access the file because it is being used by another process.


please provide me some solutions
Posted

Can we see a code that is responsible for sending a PDF file via e-mail? Your problem might be caused by not released memory stream. If you're using an Attachment class then you should do like the following:

C#
using (Attachment data = new Attachment("document.pdf",  MediaTypeNames.Application.Octet))
{
    // 1. Adding attachment to the e-mail message
    // 2. Sending out the e-mail message
}


After calling the Send method make a call to Dispose of your mail message object:

C#
objSMTPClient.Send(objMailMsg); 
objMailMsg.Dispose(); 
 
Share this answer
 
Comments
VJ Reddy 20-Jun-12 22:37pm    
Good answer. 5!
Manas Bhardwaj 21-Jun-12 3:15am    
thx!
In .net 3.5

C#
using (Process proc = new Process())
               {
                   proc.StartInfo.UseShellExecute = false;
                   proc.StartInfo.CreateNoWindow = true;
                   proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                   proc.StartInfo.FileName = "ABC.EXE";
                   proc.StartInfo.Arguments = "Report 06202012 42554 PM.pdf";
                   proc.StartInfo.RedirectStandardError = false;
                   proc.StartInfo.RedirectStandardOutput = false;
                   proc.StartInfo.UseShellExecute = false;
                   proc.StartInfo.CreateNoWindow = true;
                   proc.Start();
                   proc.WaitForExit();
               }


proc.WaitForExit(); This is the key


In CMD:
XML
Use the "Start /wait <command>". <command> = D:\ABC Report 06202012 42554 PM.pdf
 
Share this answer
 
v2
Comments
Sandeep Mewara 20-Jun-12 7:54am    
Can you elaborate a little please?
Roman Lerman 20-Jun-12 10:11am    
As I understand you are using C#. The code above is executing the abc.exe tool with "Report 06202012 42554 PM.pdf" as argument from your example "ABC Report 06202012 42554 PM.pdf". The proc.WaitForExit() is used to make the current thread wait until the associated process terminates.[^]
Right after the "using" block the ABC process is finished and the PDF document should be released(if it is not locked by some other process), it's mean that you can delete, move or change it.

If you want to delete the pm.pdf document your code should look like this:
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.FileName = "ABC.EXE";
proc.StartInfo.Arguments = "Report 06202012 42554 PM.pdf;
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
}
File.Delete("PM.pdf");

sorry for my English
Sandeep Mewara 21-Jun-12 16:44pm    
Have 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900