简体   繁体   English

File.Move的原子性

[英]Atomicity of File.Move

I want to rename a file in a directory as an atomic transaction. 我想将目录中的文件重命名为原子事务。 The file will not be changing directories. 该文件不会更改目录。 The path is provided as a UNC Path to an NTFS file system, probably on either Server 03 or 08. 该路径作为NTFS文件系统的UNC路径提供,可能在Server 03或08上。

Is File.Move() atomic for these purposes? File.Move()是否原子用于这些目的? As in, it either completes successfully or fails such that the original file is still intact? 如果它成功完成或失败,原始文件仍然完好无损?

My gut says yes, but I wanted to make sure. 我的直觉是肯定的,但我想确定一下。

Yes, in NTFS. 是的,在NTFS中。 From here : 这里

As an aside if you are running under NTFS then file operations are atomic at the file system level. 另外,如果您在NTFS下运行,那么文件操作在文件系统级别是原子操作。 A rename will occur in a single operation as far as any higher code is concerned. 就任何更高的代码而言,重命名将在单个操作中发生。 The problem you are seeing almost appears to be an issue where the FileInfo object is being shared across applications. 您看到的问题几乎似乎是跨应用程序共享FileInfo对象的问题。 It is a MarshalByRef object and therefore can be used in remoting environments. 它是MarshalByRef对象,因此可用于远程处理环境。 Don't know if this applies to you. 不知道这是否适用于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM