Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am getting this message when i am trying delete pdf file.

An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code

Additional information: The process cannot access the file xxxx because it is being used by another process."

C#
public PdfView(PdfViewModel viewModel)
        {
            InitializeComponent();
            this.DataContext = viewModel;
            string Exepath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string Directory = System.IO.Path.GetDirectoryName(Exepath);
            string Path =  Directory + "\\Payslip.pdf"
           webBrowser1.Navigate(new System.Uri(Path));
          

          
               if (System.IO.File.Exists(Path))
               {
                   File.Delete(Path);
               }
           }

}


can somebody help me with this nigtmare?

Thanks in advance
Posted
Updated 27-Nov-14 9:09am
v4
Comments
PIEBALDconsult 27-Nov-14 15:11pm    
If you've navigated to it, it's open in the browser, yes? It's in use. Close the browser.
Or, (if I recall correctly) read the file into memory and set the Document property rather than navigating. Maybe.

1 solution

If a resource is accessed by a process then it will have a lock.So no other process can access the resource in this case. So make sure when you access the file no other processes are accessing the file. There is no way to fix this issue. Either you have to kill the process which is using the file or leave it like that. In your case make a copy of the file and use it. have 2 copies of file and delete once the processing is done.
 
Share this answer
 

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