0

Possible Duplicate:
How do you get the file size in C#?

How can I calculate the size of any file in C++ or Visual C++/.NET or C#?

I mean a .exe, .txt, .doc, etc. file.

Cœur
  • 37,241
  • 25
  • 195
  • 267
hero
  • 11
  • 1
  • 2

5 Answers5

3

What do you mean "calculate"?

You just ask the file system, "how big is that file?", and it gives you the length, no calculation involved.

What is your question exactly?

How to do that in C#?

Here's code that will give you the length of a file, there are other ways as well:

long length = new FileInfo(@"c:\temp\test.exe").Length;
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
1

You can do it in C++ this way:

Taken directly from cplusplus.com

// Obtaining file size
#include <iostream>
#include <fstream>
using namespace std;

int main () {
    long begin,end;
    ifstream myfile ("example.txt");
    begin = myfile.tellg();
    myfile.seekg (0, ios::end);
    end = myfile.tellg();
    myfile.close();
    cout << "size is: " << (end-begin) << " bytes.\n";
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GWW
  • 43,129
  • 11
  • 115
  • 108
0

If you just want the size of the File, you can do this in C#:

long size = new System.IO.FileInfo("<path to file>").Length;
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • I work hiding data in Ip packet I will divid the file to bits for that I want know the length – hero Mar 03 '11 at 19:49
0

Another variant that matches your request -- Visual C++ using Windows File Management API:

LARGE_INTEGER liFileSize;
HANDLE hFile;
// open/create file 
GetFileSizeEx(hFile, &liFileSize);
// close handle
Adam Holmberg
  • 7,245
  • 3
  • 30
  • 53
0

Use stat.

struct stat buf; fstat(fd, &buf); int size = buf.st_size;

ultifinitus
  • 1,813
  • 2
  • 19
  • 32