Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 How can we find file size in C#.NET?

Author  Topic 

Mamatha
Posting Yak Master

102 Posts

Posted - 2005-05-09 : 03:09:39
Hi

I want to know the size of a file in C#.NET,i am new to C#.net.
Please give me the solution to find size of a particular file in C#.NET.

Thanks in advance

Mamatha

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-05-09 : 03:36:46
It's in the documentation. Do you have the SDK docs installed ?

System.IO.FileInfo fi = new System.IO.FileInfo(filepath);
fi.Length();



Damian
Ita erat quando hic adveni.
Go to Top of Page

Denverjames2009
Starting Member

1 Post

Posted - 2009-01-02 : 17:04:07
You know what I hate most about people? When they answer a question with, "it's in the documentation" like they are trying to make you feel stupid. If it's such an inconvenience to answer, and you're just going to refer someone to "the documentation", then don't even bother! This world is a lot more content without people like you! Please, just stay under your rock.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2009-01-02 : 17:10:34
maybe he forgot to look in the documentation and needs stating the obvious

___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.1 out!
Go to Top of Page

jinlye
Starting Member

1 Post

Posted - 2009-10-30 : 06:46:30
If Merkin had the documentation he might have seen that .Length is a property and not a method, so doesn't take brackets after it.
Go to Top of Page

May Chan
Starting Member

6 Posts

Posted - 2009-11-26 : 07:15:25
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());
}
}



____________
www.absolutesteal.com
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2009-11-26 : 17:50:53
whats wrong with giving someone a pointer towards documentation ?

Go to Top of Page

behrman
Yak Posting Veteran

76 Posts

Posted - 2009-12-01 : 10:35:11
I think May Chan is right.

RAQ Report: Web-based Excel-like Java reporting tool
Go to Top of Page

Paul daniel
Starting Member

3 Posts

Posted - 2009-12-28 : 04:46:16
Hi

To get the filesize(bytes,kilobytes,megabytes or GB] Check this code..

public static string GetFileSize(long Bytes)
{
if (Bytes >= 1073741824)
{
Decimal size = Decimal.Divide(Bytes, 1073741824);
return String.Format("{0:##.##} GB", size);
}
else if (Bytes >= 1048576)
{
Decimal size = Decimal.Divide(Bytes, 1048576);
return String.Format("{0:##.##} MB", size);
}
else if (Bytes >= 1024)
{
Decimal size = Decimal.Divide(Bytes, 1024);
return String.Format("{0:##.##} KB", size);
}
else if (Bytes > 0 & Bytes < 1024)
{
Decimal size = Bytes;
return String.Format("{0:##.##} Bytes", size);
}
else
{
return "0 Bytes";
}


}
Go to Top of Page

behrman
Yak Posting Veteran

76 Posts

Posted - 2010-01-03 : 10:44:23
You can use System.IO.FileInfo

example:
System.IO.FileInfo fileInfo = new System.IO.FileInfo(@"c:\getmysize.txt");
// write file length in bytes to textbox1
this.textBox1.Text = fileInfo.Length.ToString();


Hope this helps,
behaman

RAQ Report: Web-based Excel-like Java reporting tool
Go to Top of Page
   

- Advertisement -