C# windows 7 file locks where file is located on a network
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C# windows 7 file locks where file is located on a network

  1. #1
    Join Date
    Jul 2012
    Posts
    7

    C# windows 7 file locks where file is located on a network

    I've been having an intermittent issue with files located on a network that I am trying to delete. I'll run into an exception where it says the file cannot be deleted because it's in use by another application.

    When I try to delete it manually from the network it gives me the same error. However, when I walk 2 feet away to another computer, logged in as a different account, i'll browse to the same location and the file will not be there at all indicating that it deleted, but must be still in use from some indexing service.

    A colleague mentioned something about the windows search service and how it indexes stuff. I disabled that and ended the SearchIndexer.exe process. That worked for a few times and then it started to reoccur randomly.

    I can end my explorer.exe process and re-run it, then manually delete the files no problem.

    It occurs when I do a Directory.Delete with recursion set to true. The majority of the time it happens with the Thumbs.db file, so it ends up not deleting the directory at all and i'm forced to end my explorer.exe and restart it in order to delete the directory.

    My question is, if the file gets deleted from the network but doesn't display as deleted on my computer, how do I prevent it from getting locked in my explorer process?

    Has anyone else ran into this before?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,439

    Re: C# windows 7 file locks where file is located on a network

    Do you access the file in your code before you attempt to delete it?

  3. #3
    Join Date
    Jul 2012
    Posts
    7

    Re: C# windows 7 file locks where file is located on a network

    In some cases yes. The files are copied to the network, and it's possible the user can re-copy them to the same directory depending on certain business circumstances.

    In otherwords, they've opened the application, copied the files, rec'd a new CD with different data but same images. When it goes to delete the directory that they were copied too, it throws an exception when trying to delete the thumbs.db file. In most cases it will delete all the images but not the directory.

    Here's the method I use, there's some logic that determines when this method is called, but the exception is caught in this method:

    Code:
    private string CreateBarcodeDirectory(string path, bool? isGreenScreen)
            {
                var returnPath = "";
                try
                {
    
                    // _barcode is set through the UI textbox
                    if (!string.IsNullOrEmpty(_barcode))
                    {
                        switch (path)
                        {
                            case @"\\isilon\ImageCapture\":
    
                                if (!Directory.Exists(path + _barcode.PadLeft(8, '0')))
                                {
                                    if (isGreenScreen == true)
                                    {
                                        var greenDirectory = _barcode.PadLeft(8, '0') + @"\OriginalGreen\";
                                        Directory.CreateDirectory(path + greenDirectory );
                                        returnPath = path + greenDirectory;
                                    }
                                    else
                                    {
                                        Directory.CreateDirectory(path + _barcode.PadLeft(8, '0'));
                                        returnPath = path + _barcode.PadLeft(8, '0');
                                    }
                                }
                                else
                                {
                                    Directory.Delete(path + _barcode.PadLeft(8, '0'), true);
                                    if (isGreenScreen == true)
                                    {
                                        var greenDirectory = _barcode.PadLeft(8, '0') + @"\OriginalGreen\";
                                        Directory.CreateDirectory(path + greenDirectory);
                                        returnPath = path + greenDirectory;
                                    }
                                    else
                                    {
                                        Directory.CreateDirectory(path + _barcode.PadLeft(8, '0'));
                                        returnPath = path + _barcode.PadLeft(8, '0');
                                    }
                                }
                                break;
                            case @"\\isilon\DCImages\":
                                if (!Directory.Exists(path + _barcode))
                                {
                                    Directory.CreateDirectory(path + _barcode);
                                    returnPath = path + _barcode;
                                }
                                else
                                {
                                    Directory.Delete(path + _barcode, true);
                                    Directory.CreateDirectory(path + _barcode);
                                    returnPath = path + _barcode;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
                catch (UnauthorizedAccessException uae)
                {
                    OnError(uae, "Unable to access the path: " + path);
                    throw;
                }
                catch (Exception ex)
                {
                    OnError(ex, "Error in CreateBarcodeDirectory(" + path + ")");
                    throw;
                }
                return returnPath;
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center