C# 中的 File 和 FileInfo 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1324788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:10:04  来源:igfitidea点击:

What is the difference between File and FileInfo in C#?

c#file

提问by bluediapente

I've been reading that the static methods of the FileClass 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 FileInfoClass if we are going to perform many operations on a specific file.

我一直在读到,类的静态方法File更适合在文件上执行小而少的任务,例如检查文件是否存在,FileInfo如果我们要对文件执行许多操作,我们应该使用类的实例一个特定的文件。

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 two different classes that seem do the same in different ways?

它们的工作方式是什么使它们适用于不同的情况?让这两个不同的类似乎以不同的方式做同样的事情有什么意义?

It would be helpful if someone could answer at least one of this questions.

如果有人可以回答至少其中一个问题,那将会很有帮助。

采纳答案by John Rasch

Generally if you are performing a single operation on a file, use the Fileclass. If you are performing multiple operations on the same file, use FileInfo.

通常,如果您对文件执行单个操作,请使用File该类。如果要对同一个文件执行多项操作,请使用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 Filemethod the check is performed.

这样做的原因是因为在访问文件时进行了安全检查。创建 的实例时FileInfo,检查仅执行一次。但是,每次使用静态File方法时都会执行检查。

回答by madcolor

The methods of the Fileand FileInfoclasses are similar, but they differ in that the methods of the File class are static, so you need to pass more parameters than you would for the methods of the FileInfoinstance.

You need to do this because it operates on a specific file; for example, the FileInfo.CopyTo()method takes one parameter for the destination path that's used to copy the file, whereas the File.Copy()method takes two parameters for the source path and the destination path."

FileFileInfo类的方法类似,但区别在于 File 类的方法是static,因此需要传递的参数比FileInfo实例的方法多。

您需要这样做,因为它对特定文件进行操作;例如,该FileInfo.CopyTo()方法采用一个用于复制文件的目标路径参数,而该File.Copy()方法采用源路径和目标路径的两个参数。”

References

参考

回答by Franci Penov

File is optimized for one-off operations on a file, FileInfo is optimized around multiple operations on the same file, but in general there isn't that much difference between the different method implementations.

File 针对文件的一次性操作进行了优化,FileInfo 针对同一文件上的多个操作进行了优化,但总的来说,不同方法实现之间没有太大区别。

If you want to compare the exact implementations, Use Reflector to look at both classes.

如果要比较确切的实现,请使用 Reflector 查看这两个类。

回答by Henk Holterman

A FileInfo may be needed to deal with Access Control properties. For the rest it is a Static versus Instance choice and you can pick what is convenient.

可能需要 FileInfo 来处理访问控制属性。其余的是静态与实例的选择,您可以选择方便的方式。

回答by Nag

FileInfo is an instance of a file thus representing the file itself. File is a utility class so can work with any file

FileInfo 是文件的一个实例,因此代表文件本身。File 是一个实用程序类,因此可以处理任何文件

回答by div

Yes, and one of the reason could be is, as Nag said Files is a utility class and hence no instance is required to be created. Same time, as File being utility class, each time require security check.

是的,原因之一可能是,正如 Nag 所说的 Files 是一个实用程序类,因此不需要创建实例。同时,由于 File 是实用程序类,每次都需要进行安全检查。

On other hand FileInfo requires instance to be created, and that point it uses security check. Thus, now performing multiple operation using FileInfo will not invoke security checks.

另一方面 FileInfo 需要创建实例,并且它使用安全检查。因此,现在使用 FileInfo 执行多个操作不会调用安全检查。

回答by brijesh

Recently I faced problem with File.Exist, I hate this function. After than I've used Fileinfoclass Existfunction then my program works correct.

最近我遇到了问题File.Exist,我讨厌这个功能。在我使用FileinfoExist函数之后,我的程序工作正常。

Actually what happen in development enviornment File.Existworks well but when it goes to live environment this function is blocking the file object due to that reason I am getting the error access denied and not able to use the file.

实际上,在开发环境中发生的事情File.Exist运行良好,但是当它进入实时环境时,此功能会阻止文件对象,因为我收到错误访问被拒绝并且无法使用该文件。

This is my learning. I will never used File.Existmethod best is to create the object and then use it. Be aware to use static methods.

这是我的学习。我永远不会使用的File.Exist方法最好是创建对象然后使用它。注意使用静态方法。

回答by Nick Wallace

The File.Existswill perform much faster than a newFileInfo(filePath).Exists- especially over a network and provided the files actually exist. This is because File.Existswill only check for existence of the file, whereas a newFileInfo(filePath).Existsfirst constructs a FileInfoobject, which contains all the properties (dates, size etc) of the file (if it exists).

File.Exists会执行速度远远超过一个新的FileInfo(filePath).Exists-尤其是在网络和提供的文件确实存在。这是因为File.Exists只会检查文件是否存在,而newFileInfo(filePath).Exists首先构造一个FileInfo对象,其中包含文件的所有属性(日期、大小等)(如果存在)。

In my experience with this, even checking for the existence of 10 files over the network is noticeably faster (ie 20ms vs 200ms) by using File.Exists.

根据我的经验,使用File.Exists.

回答by DragneelFPS

FileInfo:

文件信息:

  • Need to instantiate before using
  • Contains instance methods
  • Cache Info about the File and you need to call Refresh every time to get the latest info about the File
  • 使用前需要实例化
  • 包含实例方法
  • 缓存有关文件的信息,每次都需要调用 Refresh 以获取有关文件的最新信息

File:

文件:

  • No need to instantiate
  • Contains static methods
  • Do not cache, so you get latest info every time you use it.
  • 无需实例化
  • 包含静态方法
  • 不缓存,因此每次使用时都会获得最新信息。

src:

源代码:

回答by Tahir77667

The major difference between Fileclass and FileInfoclass is that

Fileclass 和FileInfoclass之间的主要区别在于

Both members of the Fileand FileInfoclass are decorated with the [System.Security.SecurityCritical] and [System.Security.SecuritySafeCritical]attribute but Fileclass has 'multiple security checks'as compared to FileInfoclass (Read Here)and the check is performed each time when you call a static member of the Fileclass.

When you create an instance of FileInfo, the check is performed only once.

FileFileInfo类的成员都用[System.Security.SecurityCritical] and [System.Security.SecuritySafeCritical]属性修饰,但与类相比,File类具有“多重安全检查” (阅读此处),并且每次调用类的静态成员时都会执行检查。FileInfoFile

创建 的实例时FileInfo,检查仅执行一次。

  • Apart from these, other minor differences are that Fileis a static type class whereas FileInfois an instance type class.
  • Therefore to access the members of FileInfoclass you need to create an instance whereas in Fileclass you can directly access its members without the need to create an instance.
  • If you are performing multiple operations on the same file, it can be more efficient to use FileInfoinstance methods instead of the corresponding static methods of the Fileclass.
  • However, Fileclass provides more methods as compared to FileInfoclass.
  • 除此之外,其他细微的区别是File静态类型类FileInfo和实例类型类。
  • 因此,要访问FileInfo类的成员,您需要创建一个实例,而在File类中,您可以直接访问其成员,而无需创建实例。
  • 如果您对同一个文件执行多个操作,使用FileInfo实例方法而不是类的相应静态方法会更有效率File
  • 但是,FileFileInfo类相比,类提供了更多的方法。

Note: Either the SecurityCriticalAttribute attribute or the SecuritySafeCriticalAttribute attribute must be applied to code for the code to perform security-critical operations.

Note: Either the SecurityCriticalAttribute attribute or the SecuritySafeCriticalAttribute attribute must be applied to code for the code to perform security-critical operations.