c# - 在 Windows 上以编程方式重命名打开的文件

标签 c# c windows linux winapi

我正在将 Unix C 应用程序移植到 Windows。此应用程序在文件打开时重命名文件,这在 Unix 上非常好,但显然它在 Windows 上不起作用。跟踪所有重命名以确保我关闭文件,然后重新打开并再次查找会很痛苦。

鉴于 Windows 资源管理器允许在使用时重命名文件,我想知道为什么我不能让它工作。我尝试过在 C 中使用 renameMoveFile,在 C# 中使用 System.IO.File.Move。它在所有情况下都失败并出现“权限被拒绝”错误(具体来说,GetLastError() 返回的错误是“该进程无法访问该文件,因为它正被另一个进程使用”)

提示?

我也尝试打开文件与 _sopen 共享。它也没有用(同样的错误)。

感谢 Stefan 的工作 C# 代码:

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();

工作 C 示例:

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");
}

最佳答案

重命名要求相关文件是用 FileShare.Delete 打开的分享。 如果缺少该共享标志,则无法在文件仍处于打开状态时重命名/移动文件。

关于c# - 在 Windows 上以编程方式重命名打开的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7147577/

相关文章:

c# - 什么在 Visual Basic 中允许而在 C# 中被禁止(反之亦然)?

java - 奇异值分解实现

c - 指针上的字符串 scanf

c - 矩阵相乘

c# - 在 C# 中验证一个字符串是否只包含字母

c# - foreach 从下往上填充

Windows 自定义 git 命令

c - 制作正弦波程序

windows - 搜索栏不适用于 Windows 10

c# - 在不加载dll的情况下获取公钥 token