Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# - Cannot Move file after Copy.

  • 15-04-2011 12:40pm
    #1
    Registered Users, Registered Users 2 Posts: 99 ✭✭


    Hi,

    I'm trying to move a file to a destination, after I have already copied it to an archive location.

    The copy command works, but then an exception is thrown by the Move command that says that:

    "the process cannot access the file because it is being used by another process"

    Is there a generic command that would close all open filehandles, or would that even be whats causing the problem?

    Thansk,
    FB


Comments

  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Post some sample code!


  • Registered Users, Registered Users 2 Posts: 99 ✭✭fun.bobby1981


    fasty wrote: »
    Post some sample code!

    string _date = DateTime.Now.Year.ToString().PadLeft(4, '0') + "-" + DateTime.Now.Month.ToString().PadLeft(2, '0') + "-" + DateTime.Now.Day.ToString().PadLeft(2, '0');
    string _archiveDir = "\\\\server\\share\\archive\\" + _date + "\\";
    Directory.CreateDirectory(_archiveDir);
    int _i = 0;
    while (_i < _fileNames.Length)
    {
    string _srcFile = _fileNames[_i];
    string _fileName = _fileNames[_i].Substring(26);
    string _destFile = "\\\\server\\share\\" + _fileName;
    string _archiveFile = _archiveDir + _fileName;

    File.Copy(_srcFile, _destFile);

    File.Move(_srcFile, _archiveFile);
    _i++;
    }

    Works fine up to File.Move command

    Thansk


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Oh yeah, this is a pain in the neck. You can either create a function that tries in a loop to open the source file and sleep for a bit or use a FileSystemWatcher to notify you when the file is copied so you can then get exclusive access to move the original.

    In terms of being notified when file handles are closed, there isn't really a .Net API for any of the native Win32 functions for watching file handles beyond FileSystemWatcher.

    Ugly hack solution:
    string path = @"c:\some\path\here\file.txt"; 
    
    while (true)
    {
        FileStream fs = null;
    
        try
        {
            fs = File.Open(path, FileMode.Open);
        }
        catch(IOException pokemon)
        {
            // So some proper checking for file locks here
            Thread.Sleep(100);
        }
        finally
        {
            fs.Dispose();
        }
    }
    


  • Registered Users, Registered Users 2 Posts: 1,691 ✭✭✭JimmyCrackCorn


    Use something to see if you still have open file handles on the file Sysinternals tools

    If its C# your process you have a race condition. :(


  • Closed Accounts Posts: 577 ✭✭✭Galtee


    Because both operations are happening in the same Thread then In theory the File. Move statement shouldn't execute until the File.Copy statement has finished unless it's being fired off in the background in a delegate by C# and I'm not sure that's the case. With this in mind the most logical answer is that C# hasn't freed up resources that it's used whilst doing File.Copy. Also it's not pest practice to just exec file operations inside loops without exception handling around the file operations as if for whatever reason a file operation terminates unexpectedly (something outside of your program has a lock on the file etc) it will bomb out of your loop before time etc. I find that whenever I'm working with files in C# I would do the following as a belt and braces operation

    try
    {
    File.Copy(_srcFile, _destFile);
    int numTrys=0;
    // Sentinel : Once file exists or numtrys>=3 this will be false
    While (!File.Exists(_destFile) && (numTrys<3))
    {
    numTrys++;
    }
    GC.Collect() // Force a garbage collection to ensure any resources cleaned up;
    File.Move(_srcFile, _archiveFile);
    }
    catch
    {
    //Log your error
    }


  • Advertisement
Advertisement