Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get File Size,File Name,File Ext In C# Windows?

Tags:

c#

winforms

im new to c#.net,any one let me know How to Get File Size,File Name,File Ext In C# Windows.

im using open file dialog in c#..im getting only the path..i dont know how to get the file name and size..

my code is :

openFileDialog1.ShowDialog();

openFileDialog1.Title = "Select The File";
openFileDialog1.InitialDirectory = "C:";
openFileDialog1.Multiselect = false;
openFileDialog1.CheckFileExists = false;

if (openFileDialog1.FileName != "")
 {
  txtfilepath1.Text = openFileDialog1.FileName;
  var fileInfo = new FileInfo(openFileDialog1.FileName);
  lblfilesize1.Text = Convert.ToString(openFileDialog1.FileName.Length);  

  lblfilesize=
  lblfilename=    
 }
like image 209
Guru G Avatar asked Oct 08 '11 07:10

Guru G


2 Answers

  • Size FileInfo.Length
  • Name FileInfo.Name
  • Extension FileInfo.Extension

You don't need to use any other class. You're already using FileInfo.

like image 116
Muhammad Hasan Khan Avatar answered Oct 13 '22 11:10

Muhammad Hasan Khan


You can use FileInfo Class. File Info

using System;
using System.IO;

class Program
{
    static void Main()
    {
    // The name of the file
    const string fileName = "test.txt";

    // Create new FileInfo object and get the Length.
    FileInfo f = new FileInfo(fileName);
    long s1 = f.Length;

    // Change something with the file. Just for demo.
    File.AppendAllText(fileName, " More characters.");

    // Create another FileInfo object and get the Length.
    FileInfo f2 = new FileInfo(fileName);
    long s2 = f2.Length;

    // Print out the length of the file before and after.
    Console.WriteLine("Before and after: " + s1.ToString() +
        " " + s2.ToString());

    // Get the difference between the two sizes.
    long change = s2 - s1;
    Console.WriteLine("Size increase: " + change.ToString());
    }
}

For Extension You can use Path.GetExtension()

like image 26
Ravi Gadag Avatar answered Oct 13 '22 11:10

Ravi Gadag