开发者

Finding File size in bit

I need E开发者_StackOverflowxact size of File.

is there anyway to find file size in bits (not byte).

I do not want to find System.IO.FileInfo.Length and convert it to bits. Because this lose one byte information .


It is not possible (in any operating system that I know of) to have files whose size is not a multiple of one byte. In other words, it is not possible for a file to have a size like “3 bytes and 2 bits”. That is why all the functionality to retrieve file length returns it in bytes. You can multiply this with 8 to get the number of bits:

long lengthInBits = new FileInfo(fileName).Length * 8;


Well, using FileInfo.Length and multiplying by 8 is the most natural way of finding a file's size in bits. It's not like it can have a length which isn't a multiple of 8 bits:

long length = new FileInfo(fileName).Length * 8;

If you want to be fancy, you could left-shift it instead:

long length = new FileInfo(fileName).Length << 3;

That's not going to make any real difference though.

There are some other ways of finding the length of the file - opening it as a stream and asking the stream for its length, for example - but using FileInfo.Length is probably the simplest. Why don't you want to use the most obvious approach? It's like asking how to add two numbers together, saying that you don't want to use the + operator, but not giving a reason why...


8 bits in a byte:

int numOfBits = myFileInfo.Length * 8;

Why do you find this to be a problem?

If you don't like the magic number:

public const int BitsInByte = 8;
int numOfBits = myFileInfo.Length * BitsInByte;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜