有没有一种方法可以使用静态方法在.NET中获取文件的大小?

编程入门 行业动态 更新时间:2024-10-06 14:25:04
本文介绍了有没有一种方法可以使用静态方法在.NET中获取文件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道获取文件大小的正常方法是使用FileInfo实例:

I know the normal way of getting the size of a file would be to use a FileInfo instance:

using System.IO; class SizeGetter { public static long GetFileSize(string filename) { FileInfo fi = new FileInfo(filename); return fi.Length; } }

是否有一种方法可以执行相同的操作而不必使用静态方法创建FileInfo实例?

Is there a way to do the same thing without having to create an instance of FileInfo, using a static method?

也许每次我想要一个文件大小时,我都试图过分地创建新实例,但是例如,尝试计算包含5000多个文件的目录的总大小.如GC所优化的那样,难道不应该有一种无需不必要地征税就可以做到这一点的方法吗?

Maybe I'm trying to be overly stingy with creating a new instance every time I want a file size, but take for example trying to calculate the total size of a directory containing 5000+ files. As optimized as the GC may be, shouldn't there be a way to do this without having to tax it unnecessarily?

推荐答案

不用担心.首先,.NET中的分配很便宜.其次,该对象将出现在gen 0中,因此应该在没有太多开销的情况下进行收集.

Don't worry about it. First, allocation in .NET is cheap. Second, that object will be in gen 0 so it should be collected without much overhead.

更多推荐

有没有一种方法可以使用静态方法在.NET中获取文件的大小?

本文发布于:2023-11-03 04:50:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   可以使用   静态   大小   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!