views:

60

answers:

1

So I have a FAT drive, lets say H: and a directory 'work' (full path 'H:\work'). I need to get the NUMBER of the first cluster of that directory. The number of the first cluster is 2-bytes value, that is stored in the 26th and 27th bytes of the folder enty (wich is 32 bytes).

Lets say I am doing it with file, NOT a directory. I can use code like this:

 static public string GetDirectoryPtr(string dir)
    {

        IntPtr ptr = CreateFile(@"H:\Work\dover.docx",
            GENERIC_READ,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            IntPtr.Zero,
            OPEN_EXISTING,
            0,//FILE_FLAG_BACKUP_SEMANTICS, 
            IntPtr.Zero);
        try
        {
            const uint bytesToRead = 2;
            byte[] readbuffer = new byte[bytesToRead];
            if (ptr.ToInt32() == -1) return String.Format("Error: cannot open direcotory {0}", dir);
            if (SetFilePointer(ptr, 26, 0, 0) == -1) return String.Format("Error: unable to set file pointer on file {0}", ptr);

            uint read = 0; // real count of read bytes
            if (!ReadFile(ptr, readbuffer, bytesToRead, out read, 0)) return String.Format("cant read from file {0}. Error #{1}", ptr, Marshal.GetLastWin32Error());                
            int result = readbuffer[0] + 16 * 16 * readbuffer[1];
            return result.ToString();//ASCIIEncoding.ASCII.GetString(readbuffer);

        }
        finally
        {
            CloseHandle(ptr);
        }
    }

And it will return some number, like 19 (quite real to me, this is the only file on the disk).

But I DONT need a file, I need a folder. So I am puttin FILE_FLAG_BACKUP_SEMANTICS param for CreateFile call... and dont know what to do next. Is there any way to get it working for a folder?

msdn is very clear on this issue http://msdn.microsoft.com/en-us/library/aa365258(v=VS.85).aspx

It sounds to me like: "There is no way you can get a number of the folder's first cluster". The most desperate thing is that my tutor said smth like "You are going to obtain this or you wont pass this course". The true reason why he is so sure this is possible is because for 10 years (or may be more) he recieved the folder's first cluster number as a HASH of the folder's addres (and I was stupid enough to point this to him, so now I cant do it the same way)

PS: This is the most spupid task I have ever had!!! This value is not really used anythere in program, it is only fcking pointless integer.

+2  A: 

So if I understand correctly, you want to read the FAT and get the first cluster of a directory on the disk?

If the above is correct, then it might be better to open the volume for direct access.
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
See the section on Phystical Disks and Volumes.

Once you have the volume open you can then read the disk sectors and scan the FAT to locate the entry for the directory you are interested in.

Here is a sample of using DeiceIoControl to read the disk geometry. http://msdn.microsoft.com/en-us/library/aa363147(VS.85).aspx

Chris Taylor
Thank, there is a chance that is exactly what i need
DarkWalker