public void Delete(string path, bool recursive) { DirInfo di = FindInfo(path); if (di == null) { throw new FileNotFoundException(string.Format("Could not find file '{0}'.", path)); } if (di.ToString() == "\\") { if (!recursive) { throw new IOException("The archive root cannot be deleted."); } StringCollection sa = new StringCollection(GetDirectories("\\")); sa.AddRange(GetFiles("\\")); foreach (string s in sa) { Delete("\\" + s, true); } } else if (di is FileInfo) { entries.Remove(di); FileInfo fi = (FileInfo)di; lock (stream) { foreach (int i in fi.seq) { stream.Position = GetOffset(i); bw.Write((short)-1); bw.Write(new byte[BLOCK_SIZE - 2]); } } } else { StringCollection sa = new StringCollection(GetDirectories(di.ToString())); sa.AddRange(GetFiles(di.ToString())); if (recursive) { foreach (string s in sa) { Delete(di.ToString() + '\\' + s, true); } } else if (sa.Count > 0) { throw new IOException("Directory is not empty."); } entries.Remove(di); if (di == current) { sa = new StringCollection(ResolvePath(di.ToString())); sa.RemoveAt(sa.Count - 1); current = FindInfo("\\" + string.Join("\\", sa.ToArray())); } } UpdateDirs(); }
public void Move(string path, string dest) { DirInfo d1 = FindInfo(path), d2 = FindInfo(dest); if (d1 == null) { throw new FileNotFoundException(string.Format("Could not find file '{0}'.", path)); } if (d2 == null) { StringCollection sa = new StringCollection(ResolvePath(dest)); string s = sa[sa.Count - 1]; sa.RemoveAt(sa.Count - 1); d2 = FindInfo("\\" + string.Join("\\", sa.ToArray())); if (d2 == null) { throw new FileNotFoundException(string.Format("Could not find file '{0}'.", dest)); } d1.Name = s; } else if (d2 is FileInfo) { throw new IOException("A file or folder with this name already exists."); } d1.Parent = d2.Index; if (d1 == current) { StringCollection sa = new StringCollection(ResolvePath(d1.ToString())); sa.RemoveAt(sa.Count - 1); current = FindInfo("\\" + string.Join("\\", sa.ToArray())); } }