如何获取单个文件大小
本文关键字:单个 文件大小 获取 何获取 | 更新日期: 2023-09-27 18:26:51
这是方法:
private void Compressions(string zipFile,string sources)
{
try
{
string zipFileName = zipFile;
string source = sources;
string output = @"c:'temp";
string programFilesX86 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) + "''Diagnostic Tool''7z.dll";
if (File.Exists(programFilesX86))
{
SevenZipExtractor.SetLibraryPath(programFilesX86);
}
else
{
string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "''7z.dll";
SevenZipExtractor.SetLibraryPath(path);
}
string programFiles = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + "''Diagnostic Tool''7z.dll";
if (File.Exists(programFiles))
{
SevenZipExtractor.SetLibraryPath(programFiles);
}
else
{
if (File.Exists(programFilesX86))
{
SevenZipExtractor.SetLibraryPath(programFilesX86);
}
else
{
string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "''7z.dll";
SevenZipExtractor.SetLibraryPath(path);
}
}
SevenZipCompressor compressor = new SevenZipCompressor();
compressor.ArchiveFormat = OutArchiveFormat.Zip;
compressor.CompressionMode = CompressionMode.Create;
compressor.TempFolderPath = System.IO.Path.GetTempPath();
string t = Path.Combine(output, zipFileName);
compressor.CompressDirectory(source, t);
this.explorerWindow = Process.Start("explorer", String.Format("/select,{0}", t));
this.TopMost = true;
}
catch (Exception err)
{
Logger.Write("Zip file error: " + err.ToString());
}
}
在底部,变量t包含目录和文件名。例如:"c:''temp''test.txt"我想得到这个文件名的大小。
我该怎么做?
由于某些原因,静态类File
不包含Size(String fileName)
方法,而是需要这样做:
Int64 fileSizeInBytes = new FileInfo(fileName).Length;
关于性能:
不要担心new FileInfo
分配:
FileInfo
不拥有任何非托管资源(即它不是IDisposable
)FileInfo
的构造函数很便宜:构造函数只需通过Path.GetFullPath
获取规范化路径并执行FileIOPermission
——它将规范化路径存储为实例字段中的.NETString
大部分工作都在Length
属性getter内部:它本身是Win32的GetFileAttributesEx
的包装器,因此执行的操作几乎与static
实用程序方法相同。
由于新的FileInfo
对象是短暂的,这意味着GC将快速将其收集为0代对象。堆上几个字符串(FileInfo
的字段)的开销实际上可以忽略不计。
Try喜欢这个
FileInfo fi = new FileInfo(fileName);
var size = fi.Length;
Console.WriteLine("File Size in Bytes: {0}", size);
例如
FileInfo fileInfo = new FileInfo(@"c:'temp'test.txt");
var size = fi.Length;
Console.WriteLine("File Size in Bytes: {0}", size);