Click here to Skip to main content
Sign Up to vote bad
good
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 - 22:02

Comments
OriginalGriff - 22 Jan '13 - 4:29
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:51
I think I know what is it about; please see my answer. —SA
Richard MacCutchan - 22 Jan '13 - 5:08
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:51
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:48
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.
Sergey Alexandrovich Kryukov - 23 Jan '13 - 3:51
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... —SA
Richard MacCutchan - 23 Jan '13 - 3:55
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.
Sergey Alexandrovich Kryukov - 23 Jan '13 - 4:00
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. —SA
Richard MacCutchan - 23 Jan '13 - 4:37
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.
Sergey Alexandrovich Kryukov - 23 Jan '13 - 4:44
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, —SA
Sergey Alexandrovich Kryukov - 23 Jan '13 - 4:02
"any interpretation may be correct" is the pretty obvious exaggeration, but "any interpretation may be incorrect", that's for sure... —SA
Kuthuparakkal - 22 Jan '13 - 5:09
unclear
Sergey Alexandrovich Kryukov - 22 Jan '13 - 16:51
I guess I know what is it — please see my answer. —SA

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:
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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 353
1 OriginalGriff 345
2 Arun Vasu 315
3 Maciej Los 208
4 Aarti Meswania 180
0 Sergey Alexandrovich Kryukov 9,755
1 OriginalGriff 7,549
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid