Hello,
You could do it by using the file FileInfo class rather than loading your file in a FileStream.
The FileInfo class will work even if the file is in use and will not throw an exception. If you use File.OpenRead(file)it will not work.
Here is a sample code:
string file = @"c:\pagefile.sys";
FileInfo f = new FileInfo(file);
long size = f.Length;
double formattedSize = size;
string[] sizes = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
Int32 sizeIndex = 0;
while (formattedSize >= 1024 && sizeIndex < sizes.Length)
{
formattedSize /= 1024;
sizeIndex++;
}
MessageBox.Show(Math.Round(formattedSize, 2).ToString() + " " + sizes[sizeIndex]);
Valery.