Programmatically rename open file on Windows
Asked Answered
V

2

18

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");
}
Vasilikivasilis answered 22/8, 2011 at 12:39 Comment(5)
what sort of failures are you experiencing?Erbil
Windows explorer allows to rename a (locked) file while it is in use? Since when?Centenary
@Doc Brown: I'm not telling open/_sopen to lock the file (no _O_EXCL flag). Quite the opposite, in fact: with _sopen I'm asking explicitly to share the file.Vasilikivasilis
If Stefan's post answered your question, you should mark it as the answer :) If it didn't you might want to explain why it didn't.Penurious
I just came across this problem (Had to rename locked file due to IIS) and used your solution from Stefan. However I kept getting permission denied when creating the FileStream. I was able to correct this issue by using FileAccess.Read instead of FileAccess.ReadWrite.Conspectus
H
22

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.

Hugh answered 22/8, 2011 at 14:23 Comment(3)
Thank you! That works in C#, now I only need to figure out the equivalent of FileShare.Delete in in C :-/Vasilikivasilis
See msdn.microsoft.com/en-us/library/aa363858%28v=vs.85%29.aspx specifically the dwShareMode parameter (FILE_SHARE_DELETE)Milagro
@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.Vasilikivasilis
L
4

It depends on how the file was opened. If a file is opened with lock, you cant write or rename it. Tools like Notepad++ open files without locking it. If you are the one who opens and edits it, you can do that too:

http://balajiramesh.wordpress.com/2008/07/16/using-streamreader-without-locking-the-file-in-c/

The code in the article shows how to use a FileStream with FileShare options:

using(FileStream fs = new FileStream(@”c:\test.txt”, FileMode.Open, FileAccess.Read,FileShare.ReadWrite))
{
    StreamReader sr = new StreamReader(fs);
    txtContents.Text = sr.ReadToEnd();
    sr.Close();
}
Leesen answered 22/8, 2011 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.