当我在Windows 7系统上运行以下简单的C程序时
#include <stdio.h>
#include <windows.h>
int main() {
int i;
CreateDirectory("testdir", NULL);
CreateDirectory("testdir\\foo", NULL);
CreateDirectory("testdir\\bar", NULL);
for (i = 0; i < 10000; i++) {
BOOL result;
result = MoveFile("testdir\\bar", "testdir\\foo\\bar");
if (!result) {
fprintf(stderr, "rename bar => foo/bar: %d\n", GetLastError());
return 1;
}
result = MoveFile("testdir\\foo", "testdir\\foo");
if (!result) {
fprintf(stderr, "rename foo => foo: %d\n", GetLastError());
fprintf(stderr, "after %d iterations\n", i);
return 1;
}
result = MoveFile("testdir\\foo\\bar", "testdir\\bar");
if (!result) {
fprintf(stderr, "rename foo/bar => bar: %d\n", GetLastError());
return 1;
}
}
return 0;
}
对MoveFile("testdir\\foo", "testdir\\foo")
的调用在随机迭代次数后失败,错误代码为5(ERROR_ACCESS_DENIED
):
C:\Users\nik>cl /O2 rename.c
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
rename.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:rename.exe
rename.obj
C:\Users\nik>rmdir /s /q testdir
C:\Users\nik>.\rename
rename foo => foo: 5
after 52 iterations
C:\Users\nik>rmdir /s /q testdir
C:\Users\nik>.\rename
rename foo => foo: 5
after 219 iterations
C:\Users\nik>rmdir /s /q testdir
C:\Users\nik>.\rename
rename foo => foo: 5
after 23 iterations
为什么?
答案 0 :(得分:1)
我发现是Windows搜索服务导致错误。解决方案是通过控制面板中的索引选项排除文件夹。