locked
Cannot delete file becouse it is used by another process. RRS feed

  • Question

  • Hi all, I dont understand my problem. In DocumentPage I upload pdf file ->I show it in dataGrid -> after select this dtg row ti create pdf file in some folder and at the same time it show it in webBrowser. After exit applicatin I am trying to delete this file but it shows me en arror mentioned in question title.

    App.xaml

    <Application ...
         Exit="App_Exit"
     >
     ...
     </Application>

    App.xaml.cs

    public partial class App : Application
    {
        void App_Exit(object sender, ExitEventArgs e)
        {
            DocumentPage z = new DocumentPage();
            z.clear();
            if (Directory.Exists("temp"))
            {
                Directory.Delete("temp", true);
            }
        }
    }

    ... here it shows me that eror message

    1. In same page (DocumentPage) I have button with function ( clear() ) : pdfDokument.Navigate("about:blank");

    after click on that button ... application exit and sucessfull delete folder temp with all files inside

    SO I dont understand why it is ok with click on that button and when I use this function in app_exit ... it doesnt work


    Thursday, June 4, 2020 11:45 AM

All replies

  • Hello,

    The object z is a local instance which means it does not release any objects create else wheres. You need to dispose of any objects are are of a higher scope .e.g. private scoped for instance.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 4, 2020 12:51 PM
  • Thanks, but how ... no idea what you mean

    ... to delete that file I have to create a bat file which delete that folder ... i really didnt know what to do

    Thursday, June 4, 2020 1:23 PM
  • Since navigation to blank page works, then try executing it programmatically. For example, if your viewer is in a window, then handle the Closing event, something like this:

    private void MyWindow_Closing( object sender, CancelEventArgs e )

    {

       pdfDokument.Navigate( "about:blank" );

    }

    It probably takes time to free the file, therefore add a loop like this:

    private void App_Exit( object sender, ExitEventArgs e )

    {

      for(; ; )

      {

        try

        {

          Directory.Delete( "temp", true );

          break;

        }

        catch( DirectoryNotFoundException )

        {

          // ignore

          break;

        }

        catch( IOException exc )

        {

          if( (uint)exc.HResult != 0x80070020 ) throw;

          Thread.Sleep( 123 );

        }

      }

    }



    • Edited by Viorel_MVP Thursday, June 4, 2020 5:08 PM
    Thursday, June 4, 2020 2:23 PM
  • Thanks, but how ... no idea what you mean

    ... to delete that file I have to create a bat file which delete that folder ... i really didnt know what to do

    it means to dispose of any objects which may have one or more files open exclusively in the folder. I can tell you what because there is not enough code shown for that.

    Don't understand why the heck you don't use File.Delete instead of a batch file.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 4, 2020 2:43 PM
  • Press the Windows key on your keyboard, type resmon.exe and press Enter.
    In the window that appears, click the CPU tab.
    In the third bar from the top, you should see the label Associated Handles.
    Type the name of the file you're trying to close into the Search Handles bar, and then click the magnifying glass icon.
    Locate the program that is using your file.
    Right-click the program using the file in question and select End Process.
    Thursday, June 4, 2020 2:53 PM
  • Thanks for your answers, indeed. 

    Why I am not useing File.Delete? It is same reason why I am posting this question. Because I can not  delete file because it is used by another process. Program delete all files but this. And *.bat file I was useing (not now) because it delete all files. I know it is not so elegant like useing code inside C# app, but it was my last option.

    I dont know if it help you, but here is some code: 

    private void dtgDocuments_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        #region inside
        if ((dtgDocuments.SelectedItem as Documents) != null)
        {
            string path = @"temp/";
            byte[] array = (dtgDocuments.SelectedItem as Documents).document;
            string name = (dtgDocuments.SelectedItem as Documents).name;
            string ext = (dtgDocuments.SelectedItem as Documents).ext.ToLower();
    
            lblDocument_name.Content = Path.GetFileNameWithoutExtension(name);
            lblDocument_ext.Content = ext;
            lblDocument_path.Content = Path.GetFullPath(path);
    
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
    
            if (File.Exists(path + name) == false)
            {
                using (FileStream f = new FileStream(path + name, FileMode.Create))
                {
                    f.Write(array, 0, array.Length);
                    f.Close();
                }
            }
    
            string full_path| = Path.GetFullPath(path + name);
            if (ext == ".jpg" || ext == ".png" || ext == ".gif")
            {
                showDocument("img");
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.UriSource = new Uri(cela_cesta);
                bi.EndInit();
                imgDocument.Source = bi;
            }
            else if (ext == ".pdf")
            {
                showDocument("pdf");
                pdfDocument.Navigate(new Uri(full_path));
            }
            else
            {
                showDocument("else");
                pdfDocument.Navigate(new Uri(full_path));
            }
        }
        #endregion
    }
    public void showDocument(string s)
    {
        imgDocument.Visibility = pdfDocument.Visibility = msDocument.Visibility = 
        inyDocument.Visibility = gridDocument_info.Visibility = Visibility.Collapsed;
    
        if (s == "img")
        { imgDocument.Visibility = gridDocument_info.Visibility = Visibility.Visible; }
        else if (s == "pdf")
        { pdfDocument.Visibility = gridDocument_info.Visibility = Visibility.Visible; }
        else if (s == "ms")
        { msDocument.Visibility = gridDocument_info.Visibility = Visibility.Visible; }
        else if (s == "else")
        { inyDocument.Visibility = gridDocument_info.Visibility = Visibility.Visible; }
    }

    just for info ... 


    Friday, June 5, 2020 7:03 AM