[RESOLVED] Finding File Size
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] Finding File Size

  1. #1
    Join Date
    Aug 2010
    Posts
    21

    [RESOLVED] Finding File Size

    How do I find and display two files sizes and then display the ratio of their sizes to each other. I have found this,
    Code:
    FileInfo wordInfo = new FileInfo(FILE_NAME);
    but do not understand how to use it. Your help would be appreciated.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    Re: Finding File Size

    Use the Length property to get the file size.

    Code:
    var fileInfo1 = new FileInfo( "filepath1" );
    long size1 = fileInfo1.Length;
    Open another FileInfo object to the second file

    Code:
    var fileInfo1 = new FileInfo( "filepath1" );
    var fileInfo2 = new FileInfo( "filepath21" );
    
    long size1 = fileInfo1.Length;
    long size2 = fileInfo2.Length;
    Use the sizes to determine the ratio.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Re: Finding File Size

    It helps nothing you only create the object and not use it physically

    You need to use the Length property of the FileInfo object to get the size of the file. In your case, you'd need something like :

    Code:
    FileInfo wordInfo = new FileInfo(FILE_NAME);
     long Result = wordInfo.Length;
    I quickly slapped something together for you. The nice thing here is that it shows KB, MB, even MB depending on the File's size

    Start a new project. Add one button, and one label to it and keep the default names, then add this :

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace GetFilesize
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public static string GetFileSizeInBytes(long TotalBytes)
            {
                if (TotalBytes >= 1073741824) //Giga Bytes
                {
                    Decimal FileSize = Decimal.Divide(TotalBytes, 1073741824);
                    return String.Format("{0:##.##} GB", FileSize);
                }
                else if (TotalBytes >= 1048576) //Mega Bytes
                {
                    Decimal FileSize = Decimal.Divide(TotalBytes, 1048576);
                    return String.Format("{0:##.##} MB", FileSize);
                }
                else if (TotalBytes >= 1024) //Kilo Bytes
                {
                    Decimal FileSize = Decimal.Divide(TotalBytes, 1024);
                    return String.Format("{0:##.##} KB", FileSize);
                }
                else if (TotalBytes > 0 & TotalBytes < 1024) //Bytes
                {
                    Decimal FileSize = TotalBytes;
                    return String.Format("{0:##.##} Bytes", FileSize);
                }
                else
                {
                    return "0 Bytes";
                }
    
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // The name of the file
                const string FileName = "C:\\INSTALL.LOG";
    
                // Create new FileInfo object and get the Length.
                FileInfo fInfo = new FileInfo(FileName);
                long Result = fInfo.Length;
    
                //Display In Label
                label1.Text = GetFileSizeInBytes(Result);
            }
        }
    }
    //Remember to add the event handler for the button

    If you have 2 files. Just follwo the same route for each file, and then you can compare the various lengths to one another.

    I hope it helps

    Hannes

    EDIT: Sorry, didn't see Arjay's excellent reply...

  4. #4
    Join Date
    Aug 2010
    Posts
    21

    Re: Finding File Size

    Thank you all for your help. I now have a better understanding of what to do. Lord bless!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center