public MainForm() { this.StartPosition = FormStartPosition.CenterScreen; FCB root = new FCB("root", FCB.FOLDER, "", 1); rootNode = new Category.Node(root); currentRoot = rootNode; category.root = rootNode; /*恢复文件管理系统*/ readFormDisk(); //读取目录信息文件 readBitMap(); //读取位图文件 readMyDisk(); //读取虚拟磁盘文件 InitializeComponent(); }
//删除内容 public void delete(string name, int type) { if (type == FCB.TXTFILE) //删除文件 { FCB deleteFCB = category.search(rootNode, name, type).fcb; category.deleteFile(name); category.delete(category.search(rootNode, name, FCB.TXTFILE )); MyDisk.deleteFileContent(deleteFCB.start, deleteFCB.size); fileFormInit(currentRoot); } else //删除文件夹 { category.deleteFolder(name); fileFormInit(currentRoot); } //目录树更新 treeView.Nodes.Clear(); createTreeView(rootNode.firstChild); }
//初始化当前结点下的界面 public void fileFormInit(Category.Node now) { labelDiskSize.Text = "当前磁盘大小: " + MyDisk.size.ToString() + "B"; labelBlockSize.Text = "当前盘块大小: " + MyDisk.blockSize.ToString() + "B"; if (now.fcb.fileName == "root") //当前目录为root时禁用返回上一层按钮 { buttonBack.Enabled = false; } else { buttonBack.Enabled = true; } textBoxSearch.Text = nowPath; //更新路径 string name = now.fcb.fileName; //窗体初始化 this.fileWindow.Init(); //按照左孩子-右兄弟树的结构, 依次显示该目录下的文件夹/文件 if (now.firstChild == null) { return; } Category.Node temp = new Category.Node(); temp = now.firstChild; fileWindow.showFiles(temp.fcb.fileName, temp.fcb.lastModify, temp.fcb.type, temp.fcb.size); temp = temp.nextBrother; while (temp != null) { FCB current = temp.fcb; fileWindow.showFiles(temp.fcb.fileName, temp.fcb.lastModify, temp.fcb.type, temp.fcb.size); temp = temp.nextBrother; } }
/* * CategoryInfo.txt中信息格式 * * 当前目录下的根节点 * 文件名称 * 文件类型 * 上次修改时间 * 文件大小 * 文件起始位置 * #分隔符 * */ /*目录信息*/ public void readFormDisk() { //把本地保存的上次的结点信息写回来 string path = Application.StartupPath + "\\CategoryInfo.txt"; if (File.Exists(path)) { StreamReader reader = new StreamReader(path); string parentName = "", Name = ""; //父结点名字,自己的名字 string lastModify = ""; //最后修改时间 int type = -1, size = 0, start = -1, infoNum = 1; //逐行读取信息 string str = reader.ReadLine(); while (str != null) { switch (infoNum) { case 1: parentName = str; infoNum++; break; case 2: Name = str; infoNum++; break; case 3: type = int.Parse(str); infoNum++; break; case 4: lastModify = str; infoNum++; break; case 5: size = int.Parse(str); infoNum++; break; case 6: start = int.Parse(str); infoNum++; break; case 7: infoNum = 1; FCB now = new FCB(Name, type, lastModify, size); category.createFile(parentName, now); //把文件结点的内容加到目录中 break; default: break; } str = reader.ReadLine(); } reader.Close(); } }
/*给文件分配空间并添加内容*/ public bool giveSpace(FCB fcb, string content) { int blocks = getBlockSize(fcb.size); if (blocks <= remain) { /*找到该文件开始存放的位置*/ int start = 0; for (int i = 0; i < blockNum; i++) { if (bitMap[i] == EMPTY) { remain--; start = i; fcb.start = i; memory[i] = content.Substring(0, blockSize); break; } } /*从该位置往后开始存放内容*/ for (int j = 1, i = start + 1; j < blocks && i < blockNum; i++) { if (bitMap[i] == EMPTY) { remain--; bitMap[start] = i; //以链接的方式存储每位数据 start = i; if (blocks != 1) { if (j != blocks - 1) { memory[i] = content.Substring(j * blockSize, blockSize); } else { bitMap[i] = END; //文件尾 if (fcb.size % blockSize != 0) { memory[i] = content.Substring(j * blockSize, content.Length - j * blockSize); } else { memory[i] = content.Substring(j * blockSize, blockSize); } } } else { memory[i] = content; } j++; //找到一个位置 } } return(true); } else { return(false); } }
/*更新文件内容*/ public void fileUpdate(int oldStart, int oldSize, FCB newFcb, string newContent) { deleteFileContent(oldStart, oldSize); //删除原内容 giveSpace(newFcb, newContent); //开辟新的块并添加内容 }