它是安全上当前正在写入文件中创建一个FileInfo? [英] Is it safe to create a FileInfo on a file that is currently being written to?

查看:273
本文介绍了它是安全上当前正在写入文件中创建一个FileInfo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序(C#4.5的WinForms应用程序)我定期检查文件夹的内容和发现存储在数据库中的任何文件的详细信息。在这个程序中,我创建使用的FileInfo 实例新的FileInfo(路径),和我读属性 CREATIONTIME LastWriteTime 的LastAccessTime 长度属性。我从来没有呼吁的FileInfo 实例的方法

In my application (C# 4.5 winforms app) I periodically check the contents of a folder and store the details of any files found into a database. Within this routine, I create a FileInfo instance using new FileInfo(path), and I read the properties CreationTime, LastWriteTime, LastAccessTime, Length and Attributes. I never call any methods on the FileInfo instance.

我想知道的是什么:有没有腐败的风险,如果一个文件正在由第三方应用程序写入锁定或运行时错误(或者被复制到被Windows文件夹的过程),而我创建的FileInfo 对象或访问它的属性?

What I want to know: is there any risk of corruption, locking or runtime errors if a file is currently being written to by a third party application (or in the process of being copied to the folder by Windows) whilst I create the FileInfo object or access it's properties?

推荐答案

没错,这就是安全。这是在一个非常低的水平,该文件系统驱动程序处理。像FAT或NTFS常见的文件系统的文件拥有的两个的磁盘上的不同的结构。首先是目录项,它存储了有关文件的元数据。象名称,时间戳,属性和长度。实际的文件数据存储在别处,那将文件数据存储集群链。

Yes, this is "safe". This is dealt with at a very low level, the file system driver. A file on common file systems like FAT or NTFS has two distinct structures on disk. First is the directory entry, it stores metadata about the file. Like the name, timestamps, attributes and length. The actual file data is stored elsewhere, a chain of clusters that stores the file data.

FileInfo的专门给你一个文件的元数据。该文件数据是敏感得多,高度随时发生变化作为过程写入文件。值得注意的是,你可以锁定访问与文件共享选项的文件数据。但是没有办法锁定的元数据。因此你总是可以得到FileInfo的一个文件,不管什么其他进程与该文件做什么。

FileInfo exclusively gives you the metadata for a file. The file data is much more sensitive, highly subject to change as a process writes to the file. Notable is that you can lock access to the file data with the FileShare options. But there's no way to lock the metadata. Accordingly you can always get the FileInfo for a file, regardless what another process is doing with the file.

当然,实际的FileInfo属性是受,而进程更改写入文件。它们被更新的懒洋洋地的,特别是房地产的LastAccessTime。如果你想确保你得到准确的信息不能再更改您需要获得该文件的锁定。通过打开与FileShare.Read或FileShare.None文件这样做。这将确保没有其他进程可以打开该文件进行写入,只要你有一个文件打开。做到注意,的可以很容易地抛出IOException,你只获得锁在没有其他进程在你面前来了,打开了书面文件。

Of course, the actual FileInfo properties are subject to change while a process writes to the file. They are updated lazily, particularly the LastAccessTime property. If you want to be sure that you've got accurate info that cannot change then you need to obtain a lock on the file. Do so by opening the file with FileShare.Read or FileShare.None. Which ensures that no other process can open the file for writing, as long as you have the file opened. Do note that that can easily throw an IOException, you'll only gain the lock when no other process came before you and opened the file for writing.

这篇关于它是安全上当前正在写入文件中创建一个FileInfo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆