Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get single file size?

Tags:

c#

winforms

This is the method:

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

In the bottom the variable t contain the directory and file name. for example: "c:\temp\test.txt" I want to get this file name size.

How can i do it ?

like image 925
DanielVest Avatar asked Aug 07 '13 07:08

DanielVest


2 Answers

For some reason the static class File does not contain a Size(String fileName) method, instead you need to do it this way:

Int64 fileSizeInBytes = new FileInfo(fileName).Length;

Regarding performance:

Don't worry about the new FileInfo allocation:

  • FileInfo does not own any unmanaged resources (i.e. it's not IDisposable)
  • FileInfo's constructor is cheap: The constructor simply gets the normalized path via Path.GetFullPath and performs a FileIOPermission - it stores the normalized path as a .NET String in an instance field.

Most of the work is inside the Length property getter: itself is a wrapper around Win32's GetFileAttributesEx - so the operations performed are almost identical to what it would be if it were a static utility method.

As the new FileInfo object is short-lived it means the GC will collect it quickly as a Generation 0 object. The overhead of a couple of strings (FileInfo's fields) on the heap really is negligible.

like image 139
Dai Avatar answered Nov 18 '22 20:11

Dai


Try likes this

FileInfo fi = new FileInfo(fileName);
var size = fi.Length;
Console.WriteLine("File Size in Bytes: {0}", size);

For example

FileInfo fileInfo = new FileInfo(@"c:\temp\test.txt");
var size = fi.Length;
Console.WriteLine("File Size in Bytes: {0}", size);
like image 35
zey Avatar answered Nov 18 '22 18:11

zey