private void ReadDirectory(int clusterStart, FAT16Directory dir)
        {
            int Cluster = clusterStart;

            while (Cluster != -1)
            {
                DiskStream.Seek(DiskBootSector.SectorOffsetForCluster(Cluster), SeekOrigin.Begin);

                byte[] dirCluster = new byte[DiskBootSector.GetBytesPerCluster()];
                DiskStream.Read(dirCluster, 0, DiskBootSector.GetBytesPerCluster());
                MemoryStream MS = new MemoryStream(dirCluster);

                ParseDirectoryCluster(dir, MS);

                Cluster = FAT_TABLE.nextClusterInChain(Cluster);
            }

            dir.ParseAllEntries();
            numeroDeArchivosLabel.Text = dir.NumeroDeArchivos().ToString();

            DirectorioActual   = dir.GetClusterStart();
            DirectorioAnterior = dir.GetParentCluster();

            UpdateFileListView(dir);
        }
        private void UpdateFileListView(FAT16Directory directory)
        {
            FileListView.Items.Clear();
            foreach (FAT16DirEntry entry in directory.GetEntries())
            {
                if (!entry.ignoreEntry)
                {
                    FileListView.Items.Add(ListViewFAT16Entry.FactoryMethod(entry));
                }
            }

            if (DirectorioAnterior == -1)
            {
                previosFolderButton.Enabled = false;
            }
            else
            {
                previosFolderButton.Enabled = true;
            }
        }
        private void ReadRootDirectory()
        {
            //Vamos a leer el Root Directory que empieza con el cluster #2
            RootDirectory = new FAT16Directory(2);

            ReadDirectory(RootDirectory.GetClusterStart(), RootDirectory);

            DirectoryTree.SelectedNode = DirectoryTree.TopNode;
            F32DirectoryTree           = new FAT16DirectoryTree(DiskBootSector.GetVolumeLabel(), 2, new List <FAT16DirectoryTree>(), true);

            foreach (FAT16DirEntry directory in RootDirectory.GetEntries())
            {
                if (directory.esDir() && !directory.ignoreEntry)
                {
                    F32DirectoryTree.GetSubDirs().Add(new FAT16DirectoryTree(directory.NombreDeDir, directory.GetFirstCluster(), null, false));
                }
            }

            DirActual = RootDirectory;
        }
        private void ParseDirectoryCluster(FAT16Directory dir, MemoryStream dirStream)
        {
            byte[] EntryByteBuffer = new byte[32];
            bool   LFNFlag         = false;

            while (dirStream.Read(EntryByteBuffer, 0, 32) > 0)
            {
                //0x00 y 0xE5 denotan que esa entrada esta libre
                if (EntryByteBuffer[0] != 0x00)
                {
                    if (EntryByteBuffer[11] == 0x0F)
                    {
                        //Entrada de directory LFN (Long File Name)
                        LFNFlag = true;

                        FAT16LFNEntry tempLFNEntry = new FAT16LFNEntry();

                        //Copiamos los datos de esa entrada de directory a una estructura LFN
                        tempLFNEntry.LDIR_Ord = EntryByteBuffer[0];
                        Array.Copy(EntryByteBuffer, 1, tempLFNEntry.LDIR_Name1, 0, 10);
                        tempLFNEntry.LDIR_ATTR     = EntryByteBuffer[11];
                        tempLFNEntry.LDIR_Type     = EntryByteBuffer[12];
                        tempLFNEntry.LDIR_Checksum = EntryByteBuffer[13];
                        Array.Copy(EntryByteBuffer, 14, tempLFNEntry.LDIR_Name2, 0, 12);
                        Array.Copy(EntryByteBuffer, 26, tempLFNEntry.LDIR_FstClustLO, 0, 2);
                        Array.Copy(EntryByteBuffer, 28, tempLFNEntry.LDIR_Name3, 0, 4);

                        TempLFNStack.Push(tempLFNEntry);
                    }
                    else
                    {
                        //Entrada de Directorio Normal
                        FAT16DirEntry tempDirEntry = new FAT16DirEntry();
                        if (EntryByteBuffer[0] == 0xE5)
                        {
                            tempDirEntry.ignoreEntry = true;
                        }
                        Array.Copy(EntryByteBuffer, 0, tempDirEntry.DIR_NAME, 0, 8);
                        Array.Copy(EntryByteBuffer, 8, tempDirEntry.DIR_EXT, 0, 3);
                        tempDirEntry.DIR_ATTR         = EntryByteBuffer[11];
                        tempDirEntry.DIR_NTRES        = EntryByteBuffer[12];
                        tempDirEntry.DIR_CrtTimeTenth = EntryByteBuffer[13];
                        Array.Copy(EntryByteBuffer, 14, tempDirEntry.DIR_CrtTime, 0, 2);
                        Array.Copy(EntryByteBuffer, 16, tempDirEntry.DIR_CrtDate, 0, 2);
                        Array.Copy(EntryByteBuffer, 18, tempDirEntry.DIR_LstAccDate, 0, 2);
                        Array.Copy(EntryByteBuffer, 20, tempDirEntry.DIR_FstClustHI, 0, 2);
                        Array.Copy(EntryByteBuffer, 22, tempDirEntry.DIR_WrtTime, 0, 2);
                        Array.Copy(EntryByteBuffer, 24, tempDirEntry.DIR_WrtDate, 0, 2);
                        Array.Copy(EntryByteBuffer, 26, tempDirEntry.DIR_FstClustLO, 0, 2);
                        Array.Copy(EntryByteBuffer, 28, tempDirEntry.DIR_FileSize, 0, 4);

                        //Clonar Stack de LFNs
                        if (LFNFlag && TempLFNStack.Count > 0)
                        {
                            tempDirEntry.ListaLFNEntries = new Stack <FAT16LFNEntry>(TempLFNStack);
                        }

                        if (TempLFNStack.Count > 0)
                        {
                            TempLFNStack.Clear();
                        }

                        //Agregar Entry a RootDirectory
                        dir.AddDirectory(tempDirEntry);
                    }
                }

                EntryByteBuffer = new byte[32];
            }

            //dir.ParseAllEntries();
        }