상위 폴더의 후속 삭제/이름 변경을 허용하는 방식으로 파일을 열 수 있습니까?(정말로) 잠그지 않고 파일을 엽니 다?
나는이 작업을 수행 할 수있어 : 파일 핸들이 닫힐 때 파일 수
File.Open("foo.bar", FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete)
가 삭제 될 수 있습니다. 그러나 오류없이 상위 폴더를 삭제할 수없는 경우
프레임 워크에서 아무것도 찾을 수 없습니다. 간과 한 것이 있거나 상호 작용할 수있는 기본 API가 있습니까?
참고 : 삭제 된 파일의 스트림을 사용할 때 예외가 발생해도 상관하지 않습니다. 사실 그것이 이상적 일 것입니다.
는 UPDATE :
그래서 가장 유망한 생각하지만 난 그냥 작동하지 수 있으며, Hardlink이었다. 부모 디렉터리를 삭제하려고하면 Access Denied로 끝납니다. 여기 내 코드는 다음과 같습니다.
class Program
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);
static void Main(string[] args)
{
string hardLinkPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
string realPath = @"C:\foo\bar.txt";
if (CreateHardLink(hardLinkPath, realPath, IntPtr.Zero))
{
using (FileStream stream = File.Open(hardLinkPath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
{
Console.Write("File locked");
Console.ReadLine();
}
File.Delete(hardLinkPath);
}
else
Console.WriteLine("LastError:{0}", Marshal.GetLastWin32Error());
}
}
내가하는 것처럼 들립니다. 파일이 충분히 작 으면 메모리에 복사 할 수 있습니까? – strager
예, 우리는 지금 그 일을하고 있습니다. 그러나 많은 파일들이 있습니다. 그리고 그들은 3Mb에서 100Mb 정도로 상당히 큽니다. 그것은 많은 GC 스 래싱을 야기합니다. –