protected virtual IFileObject GetUniqueFile(IFileSystem fs, string fileName) { int fileNum = 0; IFileObject uniqueFile = null; do { fileNum++; var extIdx = fileName.LastIndexOf('.'); var newFileName = extIdx >= 0 ? String.Format("{0}{1}{2}", fileName.Substring(0, extIdx), fileNum, fileName.Substring(extIdx)) : fileName + fileNum.ToString(); uniqueFile = fs.ResolveFile(newFileName); } while (uniqueFile.Exists() && fileNum < 100); if (uniqueFile.Exists()) { var extIdx = fileName.LastIndexOf('.'); var uniqueSuffix = Guid.NewGuid().ToString(); uniqueFile = fs.ResolveFile( extIdx >= 0 ? fileName.Substring(0, extIdx) + uniqueSuffix + fileName.Substring(extIdx) : fileName + uniqueSuffix); } return(uniqueFile); }
protected void prepeare(IFileSystem fileSystem) { // create folder IFileObject testFolder = fileSystem.ResolveFile("test"); testFolder.CreateFolder(); // create 10 files for (int i = 0; i < 10; i++) { string fName = String.Format("test/test{0}.{1}", i, i % 2 == 0 ? "txt" : "doc"); IFileObject testFile = fileSystem.ResolveFile(fName); testFile.CreateFile(); StreamWriter streamWr = new StreamWriter(testFile.Content.GetStream(FileAccess.Write)); streamWr.Write("This is test content #" + i.ToString()); streamWr.Close(); } // GetChildren if (testFolder.GetChildren().Length != 10) { throw new Exception("GetChildren failed"); } // create another dir with subdir IFileObject testTest2Folder = fileSystem.ResolveFile("test2/test/"); testTest2Folder.CopyFrom(testFolder); testTest2Folder.Parent.CopyFrom(testFolder); // FindFiles IFileObject[] txtTest2Files = testTest2Folder.Parent.FindFiles(new MaskFileSelector("*.txt")); if (txtTest2Files.Length != 10) { throw new Exception("FindFiles failed"); } foreach (IFileObject f in txtTest2Files) { f.Delete(); } if (testTest2Folder.GetChildren().Length != 5) { throw new Exception("Delete failed"); } // check copied file content IFileObject test1docFile = fileSystem.ResolveFile("test2/test/test1.doc"); if (!test1docFile.Exists()) { throw new Exception("ResolveFile failed"); } StreamReader rdr = new StreamReader(test1docFile.Content.GetStream(FileAccess.Read)); string content = rdr.ReadToEnd(); rdr.Close(); Assert.AreEqual("This is test content #1", content); // deep tree copy test IFileObject test3Folder = fileSystem.ResolveFile("test3"); test3Folder.CopyFrom(fileSystem.ResolveFile("test2")); // count doc files if (fileSystem.Root.FindFiles(new MaskFileSelector("*.doc")).Length != 25) { throw new Exception("CopyFrom (subtree) or FindFiles failed"); } }