0

I've an application which after generate a file, tries to move it from a network location to a local folder using the File.Move function .

File.Move(_backupSource + @"\" + filename, _backupDestination + @"\" + filename);

_backupSource is the network location and _backupDestintation is the local folder.

The file is copied to the destination location, from a few days is not deleting the file on the Source, like if I used File.Copy, but this aplication was working fine for two months now.

I also looked at the event viewer from the two servers and no exception was generated.

¿What could happen?

EDIT: One thing that could be important. This app is triggered by the task scheduler every night. If I run the aplication manually, the file is moved (not copied)

EDIT 2: Now I've tried to copy the file and then delete the file using the following method

private static void MoveFiles(string filename)
{
    FileInfo file = new FileInfo(_backupSource + @"\" + filename);

    FileStream stream = null;

    int iteration = 0;

    file.CopyTo(_backupDestination + @"\" + filename);

    bool isUnlocked = false;
    while (!isUnlocked)
    {
        iteration++;
        try
        {
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            isUnlocked = true;
        }
        catch (IOException ex)
        {
            //the file is unavailable because it is:
            //still being written to
            //or being processed by another thread
            //or does not exist (has already been processed)
            Thread.Sleep(60 * 1000);
            if (iteration > 60)
                throw ex;
            else
                EventLog.WriteEntry("DailyRestore", string.Format("Move File. Iteration number {0}", filename), EventLogEntryType.Information);

        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
    }


    file.Delete();
}

In the log can see the app was trying by an hour each minute and the file is always "using by another process". Like I mention, this is when the exe is executed by the scheduler task, if I run the task that execute the exe manually (right click and execute), all the operations are successfully ended.

Mariano G
  • 255
  • 4
  • 14
  • the account under which app is running has delete rights in the source folder? – mslliviu Mar 26 '15 at 14:33
  • Yes, is a domain account wich is administrator on both servers – Mariano G Mar 26 '15 at 14:36
  • Maybe some kind of authorization/access has changed? If you only wish to make sure the file is gone, use if(File.Exists(_backupSource + @"\" + filename)) { File.Delete(_backupSource + @"\" + filename); } – Justjyde Mar 26 '15 at 14:36
  • 1
    Side note: Rather use [Path.Combine](https://msdn.microsoft.com/en-us/library/dd784047%28v=vs.110%29.aspx) instead of concatenating with `+`. – Filburt Mar 26 '15 at 14:40
  • Try the code Justjyde wrote, but add some exception handling and logging for the File.Delete part, see if you get more details. I had a simillar issue when WinRar process (used to compress backup before move) was not closing and not releasing the handle on the file. – mslliviu Mar 26 '15 at 14:40
  • mslliviu, I'm handling exceptions, and the method doesn't throw any. Also I added log when the "file.Move" ends, a "successfully" entry, so it seems to be all ok. Justjyde, if I run the application manually, works fine, using the same user. It seems to be something with the scheduler, but I'm not sure what. – Mariano G Mar 26 '15 at 14:49
  • 1
    MSDN: _If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source._ – TaW Mar 26 '15 at 15:12
  • I used something like this code [link]http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use[link] to ensure when the file is free to move and added log entries to know what happened and all seems to be fine, the file is created by the app so it should be free to move when ends the creation, but keeps copying the file instead of move. – Mariano G Mar 31 '15 at 12:37

1 Answers1

0

Try to run application as administrator by create command file to run the application as administrator instead of calling the application

  1. Create new text file "start.cmd"
  2. Open the file as a text and Write the command runas.exe /savecred /user:YourUser "YourAppFullPath"
  3. Now Run "start.cmd" once to enter the user password. it will be saved until the user change it
  4. Then you can call "start.cmd" instead of calling your application
Ahmed Abdallah
  • 417
  • 4
  • 7