Programmatically rename open file on Windows

10,171

Renaming requires that the file in question was opened with FileShare.Delete sharing. If that share flag is missing, you can not rename/move the file while it is still open.

Share:
10,171
pgquiles
Author by

pgquiles

Updated on June 07, 2022

Comments

  • pgquiles
    pgquiles almost 2 years

    I am porting a Unix C application to Windows. This application renames files while they are open, which is perfectly fine on Unix but apparently it does not work on Windows. Tracing all the renames to make sure I close the file, then reopen and seek again would be painful.

    Given that Windows Explorer allows to rename a file while is in use, I wonder why I cannot get this to work. I have tried with rename and MoveFile in C, and System.IO.File.Move in C#. It fails in all cases with a "Permission denied" error (specifically, the error returned by GetLastError() is "The process cannot access the file because it is being used by another process")

    Tips?

    I have also tried to open the file for sharing with _sopen. It didn't work either (same error).

    Working C# code thanks to Stefan:

    string orig_filename = "testrenamesharp-123456";
    string dest_filename = "fancynewname.txt";
    Byte[] info = new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method.");
    var fs = new FileStream(orig_filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete);
    fs.Write(info, 0, info.Length);
    File.Move(orig_filename, dest_filename);
    fs.Close();
    

    Working C sample:

    const char* filename = "testrename-XXXXXX";
    const char* dest_filename = "fancynewname.txt";
    
    /* The normal POSIX C functions lock the file */
    /* int fd = open(filename, O_RDWR | O_CREAT, _S_IREAD | _S_IWRITE); */ /* Fails */
    /* int fd = _sopen(filename, O_RDWR | O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); */ /* Also fails */
    
    /* We need to use WINAPI + _open_osfhandle to be able to use 
       file descriptors (instead of WINAPI handles) */
    HANDLE hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL );
    if( INVALID_HANDLE_VALUE == hFile) {
        ErrorExit(TEXT("CreateFile"));
    }
    
    int fd = _open_osfhandle((long int)hFile, _O_CREAT | _O_RDWR | _O_TEMPORARY);
    if( -1 == fd ) {
        perror("open");
    }
    
    int resw = write(fd, buf, strlen(buf));
    if(-1 == resw) {
        perror("write");
    }
    
    if( 0 == access(dest_filename, F_OK)) {
        perror("access");
    }
    
    /* Now try to rename it - On Windows, this fails */
    int resr = rename(filename, dest_filename);
    if( -1 == resr) {
        perror("rename");
    }
    
    int resc = close(fd);
    if( -1 == resc ) {
        perror("close");
    }
    
  • pgquiles
    pgquiles over 12 years
    Thank you! That works in C#, now I only need to figure out the equivalent of FileShare.Delete in in C :-/
  • Joe
    Joe over 12 years
    See msdn.microsoft.com/en-us/library/aa363858%28v=vs.85%29.aspx specifically the dwShareMode parameter (FILE_SHARE_DELETE)
  • pgquiles
    pgquiles over 12 years
    @Joe: Thank you, I had just found about that and finished testing. I've edited the C sample. I needed to use _open_osfhandle too because the app uses filedescriptors.