Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Get the block and size from the file in system.
Read & Write the this block in same file.
Posted
Comments
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.
Sergey Alexandrovich Kryukov 22-Jan-13 16:51pm    
I think I know what is it about; please see my answer.
—SA
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.
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.
—SA
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.

1 solution

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:
C#
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"]);
                System.Console.WriteLine();
            } // loop


How it works?

Look at the class ManagementObject:
http://msdn.microsoft.com/en-us/library/system.management.managementobject.aspx[^].

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
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394515%28v=vs.85%29.aspx[^].

Good luck,
—SA
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900