是什么在C#中的文件和FileInfo的区别? [英] What is the difference between File and FileInfo in C#?
问题描述
我一直在读的静态方法文件的类最好是用在像检查,看看是否存在对文件进行小一些任务,我们应该使用实例在的FileInfo 类,如果我们要在一个特定的文件执行许多操作。
I've been reading that the static methods of the File Class are better used to perform small and few tasks on a file like checking to see if it exists and that we should use an instance of the FileInfo Class if we are going to perform many operations on a specific file.
我理解这一点,可以简单地使用这种方式盲目的,但我想知道为什么是有区别吗?这是关于他们的工作,使它们适用于不同情况的方式呢?什么是具有似乎做同样以不同的方式这2个不同类别的地步?
I understand this and can simply use it that way blindly but I would like to know why is there a difference ? What is it about the way they work that make them suitable for different situations ? What is the point of having this 2 different classes that seem do the same in different ways ?
这将是有益的,如果有人能回答这个问题的至少一个。
It would be helpful if someone could answer at least one of this questions.
推荐答案
一般来说,如果你在一个文件执行单个操作,使用文件
类。如果在同一个文件进行多次操作,可以使用的FileInfo
。
Generally if you are performing a single operation on a file, use the File
class. If you are performing multiple operations on the same file, use FileInfo
.
之所以做这种方式是因为安全的访问文件时,检查完成的。当您创建的FileInfo
的一个实例,检查只执行一次。不过,每次使用一个静态文件进行检查
方法。
The reason to do it this way is because of the security checking done when accessing a file. When you create an instance of FileInfo
, the check is only performed once. However, each time you use a static File
method the check is performed.
这篇关于是什么在C#中的文件和FileInfo的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!