簡體   English   中英

我如何獲得單個文件大小?

[英]How do i get single file size?

這是方法:

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 - 它將規范化路徑存儲為實例字段中的.NET String

大多數工作都在Length屬性getter中:它本身是Win32的GetFileAttributesEx的包裝器 - 因此執行的操作幾乎與它是static實用程序方法時的操作相同。

由於新的FileInfo對象是短暫的,這意味着GC將作為Generation 0對象快速收集它。 堆上的幾個字符串( FileInfo的字段)的開銷實際上可以忽略不計。

試試喜歡這個

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM