Click here to Skip to main content
12,747,137 members (30,662 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
Get the block and size from the file in system.
Read & Write the this block in same file.
Posted 21-Jan-13 23:02pm
OriginalGriff 22-Jan-13 4:29am
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 22-Jan-13 5:08am
The operating system has no concept of blocks; files are merely streams of bytes. Block size depends on the applications that read and write the files.
Kuthuparakkal 22-Jan-13 5:09am
Sergey Alexandrovich Kryukov 22-Jan-13 16:51pm
I think I know what is it about; please see my answer.
Sergey Alexandrovich Kryukov 22-Jan-13 16:51pm
You are right, but the file system has a certain block size, so, this is a per-volume characteristic.
Please see my answer.
Sergey Alexandrovich Kryukov 22-Jan-13 16:51pm
I guess I know what is it — please see my answer.
Richard MacCutchan 23-Jan-13 3:48am
Yes, you are right, but that has nothing to do with OP's question, which is how to get the block size of a file. The physical block size of disk volumes is largely irrelevant in user applications.
Well, this is the only way I could understand this question, but how do you understand it?
As to "largely irrelevant", it's true. That's why it is not available in regular .NET API for applications...
Richard MacCutchan 23-Jan-13 3:55am
Like I said OP is asking for file block size. But that is unique to every file and there is no standard for how, or even whether, the information is stored anywhere in the file.

To be honest, the question is so vague that any interpretation may be correct. Perhaps we'll never know.
I would rather say, files have no block sizes, don't you agree? Or do you mean the size of the buffer? But that would not even depend on the file...
Therefore, I think the only relevant notion is the filesystem block size. When you format a volume, you provide the volume file system format, label, and optional block size. Is there anything else?

However, if you think that the notion of "file block size" exist and I'm missing it, please tell me; I would like to know about it.

"any interpretation may be correct" is the pretty obvious exaggeration, but "any interpretation may be incorrect", that's for sure...
Richard MacCutchan 23-Jan-13 4:37am
You can create a file and write its content as fixed or variable sized blocks, which are totally independent of the physical filesystem block size. However, such block sizes are unique to the program that creates, reads and updates the file. And even if it is stored in the file itself there is no standard way of determining it. This is very common for Databases and mainframe applications.

So yes, your assertion that "files have no block sizes", is quite correct.
OK, I understand all that. Thank you for confirmtaion.

Then you should understand my motivation for choosing the volume block size, because actually this is the only persistent characteristic with such name we know for now (its lifetime is the lifetime of the volume formatting).

If you understand just this motivation, this would be quite enough, because everything beyond would be some useless speculations. We already discussed all which could possibly be involved and probably agree.

I would draw a line here, if you don't mind, of course.

Thank you very much,

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

First of all, there is no a single block size. This is a characteristic if each volume, not of the whole system.

This is not trivial thing at all. I prefer to show how to do it with WMI, because, this way, you can do it in pure C#.

Consider the following code which shows you the block size of each volume:
using System.Management;


            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Volume");
            ManagementObjectCollection collection = searcher.Get();
            foreach (ManagementObject item in collection) {
                System.Console.WriteLine("Name: {0}", item["Name"]);
                System.Console.WriteLine("Block size: {0}", item["BlockSize"]);
            } // loop

How it works?

Look at the class ManagementObject:[^].

As you can see, this class provides access to different properties of a run-time management object via the indexed property '[]', indexed by strings. How do you know what property to use?
The class provides reflective API; for example, you can use the property Properties (and Methods, so you can invoke methods). This class is actually a wrapper over some "real" management objects you can get via a query.

But where to read about the meaning of the properties and method? You can use C++ documentation to get the properties you want. For Win32_Volume, use[^].

Good luck,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100