private void LoadDirContents(string path) { string[] dirs = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly); string[] files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly); // Add the parent folder only if the inital name is not /* if (!path.Equals(GetSDCardPath())) { ShortLongFileName slf = new ShortLongFileName(); DirectoryInfo parent = Directory.GetParent(path); slf.longName = parent.ToString(); slf.shortName = ".." + spaces + spaces[0] + (char)(byte)FileAttributes.Directory + spaces + spaces + "\0\0\0\0"; slf.isDirectory = true; dircontent.Add(slf); } foreach (string dir in dirs) { ShortLongFileName slf = new ShortLongFileName { longName = dir, shortName = ShortFilename(dir.Substring(path.Length)) + (char)(byte)FileAttributes.Directory + spaces + spaces + "\0\0\0\0", isDirectory = true }; dircontent.Add(slf); } foreach (string file in files) { int size = (int)new FileInfo(file).Length; byte[] sizeB = BitConverter.GetBytes(size); ShortLongFileName slf = new ShortLongFileName { longName = file, shortName = ShortFilename(file.Substring(path.Length)) + (char)(byte)FileAttributes.Archive + spaces + spaces + System.Text.Encoding.Default.GetString(sizeB) }; while (ListContains(dircontent, slf.shortName.Substring(0, 11))) { if (slf.shortName.Substring(7, 1).Equals("\0")) { break; } int fileVal = Convert.ToInt32(slf.shortName.Substring(7, 1)); fileVal++; slf.shortName = slf.shortName.Substring(0, 7) + fileVal + slf.shortName.Substring(8); } dircontent.Add(slf); } }
/* __________________________________________________________________________________________________________ * | 00 - 07 | 08 - 0A | 0B | 0C | 0D | 0E - 0F | 10 - 11 | 12 - 13| 14 - 15 | 16 - 17 | 18 - 19 | 1A - 1B | 1C - 1F | * |Filename |Extension|File attrib|User attrib|First ch del|Create time|Create date|Owner ID|Acc rights|Mod. time|Mod. date|Start cluster| File size | * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ private void ReadFile(string name) { if (name.Contains("*")) { fileToReadAsBytes = null; // Path is compounded, as long as "CLOSE" is not called string rootPath = GetSDCardPath(); if (sdCurrentPath.Length > 0) { rootPath = sdCurrentPath; } dircontent.Clear(); LoadDirContents(rootPath); } else { string fileToOpen = name; if (fileToOpen.StartsWith("/")) { fileToOpen = fileToOpen.Substring(1).Replace("/*", ""); } // we only store the name of the file, not the path int lastSlash = fileToOpen.LastIndexOf("/"); if (lastSlash > 0) { fileToOpen = fileToOpen.Substring(lastSlash + 1); } ShortLongFileName slf = FindByShortName(fileToOpen); if (slf != null) { if (slf.isDirectory) { sdCurrentPath = slf.longName; } else { dircontent.Clear(); dircontent.Add(slf); fileToReadAsBytes = slf.longName; data[0] = (byte)CH376SResponse.CMD_STAT_SUCCESS; } } } }