private static void doCopyDir(string sourceFolder, string destFolder, bool ovewriteExisting) { // https://stackoverflow.com/a/3911658/107625 if (!Directory.Exists(destFolder)) { Directory.CreateDirectory(destFolder); } // Get Files & Copy var files = Directory.GetFiles(sourceFolder); foreach (var file in files) { var name = Path.GetFileName(file); // ADD Unique File Name Check to Below. var dest = ZspPathHelper.Combine(destFolder, name); File.Copy(file, dest, ovewriteExisting); } // Get dirs recursively and copy files var folders = Directory.GetDirectories(sourceFolder); foreach (var folder in folders) { var name = Path.GetFileName(folder); var dest = ZspPathHelper.Combine(destFolder, name); doCopyDir(folder, dest, ovewriteExisting); } }
public void TestTilde() { // https://github.com/UweKeim/ZetaLongPaths/issues/24 var path1 = ZspIOHelper.GetTempDirectory().CombineDirectory(Guid.NewGuid().ToString()).CheckCreate(); var path2 = Directory.CreateDirectory(ZspPathHelper.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())); try { var p1 = path1.CombineDirectory(@"a~b").CheckCreate(); var p2 = Directory.CreateDirectory(ZspPathHelper.Combine(path2.FullName, @"a~b")).FullName; var f1 = p1.CombineFile("1.txt"); f1.WriteAllText("1"); var f2 = ZspPathHelper.Combine(p2, "1.txt"); File.WriteAllText(f2, "1"); foreach (var file in p1.GetFiles()) { Console.WriteLine(file.FullName); } foreach (var file in Directory.GetFiles(p2)) { Console.WriteLine(file); } } finally { path1.SafeDelete(); path2.Delete(true); } }
public void TestGeneral5() { var s1 = @"c:\folder1\folder2\folder3\file1.txt"; var s2 = @"c:\folder1\folder2\folder3\file1.txt"; Assert.IsTrue(ZspPathHelper.AreSameFilePaths(s1, s2)); s1 = @"c:\folder1\folder2\folder3\file1.txt"; s2 = @"c:\folder1\folder2\folder3\file2.txt"; Assert.IsFalse(ZspPathHelper.AreSameFilePaths(s1, s2)); s1 = @"c:\folder1\folder2\folder3\file1.txt"; s2 = @"c:\folder1\folder2\folder4\file1.txt"; Assert.IsFalse(ZspPathHelper.AreSameFilePaths(s1, s2)); s1 = @"c:\folder1\folder2\folder3\folder4\..\file1.txt"; s2 = @"c:\folder1\folder2\folder3\file1.txt"; Assert.IsTrue(ZspPathHelper.AreSameFilePaths(s1, s2)); s1 = @"c:\folder1\folder2\folder3\folder4\..\..\folder3\file1.txt"; s2 = @"c:\folder1\folder2\folder3\file1.txt"; Assert.IsTrue(ZspPathHelper.AreSameFilePaths(s1, s2)); }
public static void SafeCopyFile( string sourcePath, string dstFilePath, bool overwrite = true) { #if WANT_TRACE Trace.TraceInformation(@"About to safe-copy file from '{0}' to '{1}' " + @"with overwrite = '{2}'.", sourcePath, dstFilePath, overwrite); #endif if (sourcePath == null || dstFilePath == null) { #if WANT_TRACE Trace.TraceInformation( string.Format( @"Source file path or destination file path does not exist. " + @"Not copying." )); #endif } else { if (string.Compare(sourcePath, dstFilePath, StringComparison.OrdinalIgnoreCase) == 0) { #if WANT_TRACE Trace.TraceInformation(@"Source path and destination path are the same: " + @"'{0}' is '{1}'. Not copying.", sourcePath, dstFilePath); #endif } else { if (SafeFileExists(sourcePath)) { if (overwrite) { SafeDeleteFile(dstFilePath); } var d = ZspPathHelper.GetDirectoryPathNameFromFilePath(dstFilePath); if (!Directory.Exists(d)) { #if WANT_TRACE Trace.TraceInformation(@"Creating non-existing folder '{0}'.", d); #endif Directory.CreateDirectory(d); } File.Copy(sourcePath, dstFilePath, overwrite); } else { #if WANT_TRACE Trace.TraceInformation(@"Source file path to copy does not exist: '{0}'.", sourcePath); #endif } } } }
public void TestGeneral3() { var s1 = @"c:\folder1\folder2\folder3\file1.txt"; var s2 = ZspPathHelper.ChangeFileNameWithoutExtension(s1, @"file2"); Assert.AreEqual(s2, @"c:\folder1\folder2\folder3\file2.txt"); s1 = @"c:\folder1\folder2\folder3\file1.txt"; s2 = ZspPathHelper.ChangeFileName(s1, @"file2.md"); Assert.AreEqual(s2, @"c:\folder1\folder2\folder3\file2.md"); }
public static void SafeMoveFile( string sourcePath, string dstFilePath) { #if WANT_TRACE Trace.TraceInformation(@"About to safe-move file from '{0}' to '{1}'.", sourcePath, dstFilePath); #endif if (sourcePath == null || dstFilePath == null) { #if WANT_TRACE Trace.TraceInformation( string.Format( @"Source file path or destination file path does not exist. " + @"Not moving." )); #endif } else { if (SafeFileExists(sourcePath)) { SafeDeleteFile(dstFilePath); var d = ZspPathHelper.GetDirectoryPathNameFromFilePath(dstFilePath); if (!Directory.Exists(d)) { #if WANT_TRACE Trace.TraceInformation(@"Creating non-existing folder '{0}'.", d); #endif Directory.CreateDirectory(d); } if (File.Exists(dstFilePath)) { File.Delete(dstFilePath); } File.Move(sourcePath, dstFilePath); } else { #if WANT_TRACE Trace.TraceInformation(@"Source file path to move does not exist: '{0}'.", sourcePath); #endif } } }
public void TestAttributes() { var tempFolder = Environment.ExpandEnvironmentVariables("%temp%"); Assert.IsTrue(Directory.Exists(tempFolder)); var tempPath = ZspPathHelper.Combine(tempFolder, "ZspTest"); try { Directory.CreateDirectory(tempPath); Assert.IsTrue(Directory.Exists(tempPath)); var filePath = ZspPathHelper.Combine(tempPath, "text.attributes.tests"); using (var textStream = new StreamWriter(new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))) { textStream.WriteLine("Zeta Long Attribute Extended testing..."); textStream.Flush(); //textStream.Close(); //fileHandle.Close(); } Assert.IsTrue(File.Exists(filePath)); // -- var now = DateTime.Now; AssertOwn.DoesNotThrow(delegate { File.SetLastAccessTime(filePath, now); }); AssertOwn.DoesNotThrow(delegate { File.SetLastWriteTime(filePath, now); }); AssertOwn.DoesNotThrow(delegate { File.SetCreationTime(filePath, now); }); Assert.AreEqual(File.GetLastAccessTime(filePath), now); Assert.AreEqual(File.GetLastWriteTime(filePath), now); Assert.AreEqual(File.GetCreationTime(filePath), now); } finally { Directory.Delete(tempPath, true); } }
public void TestGeneral6() { var s1 = @"c:\folder1\folder2\folder3"; var s2 = @"c:\folder1\folder2\folder3"; Assert.IsTrue(ZspPathHelper.AreSameFolderPaths(s1, s2)); s1 = @"c:\folder1\folder2\folder3"; s2 = @"c:\folder1\folder2\folder4"; Assert.IsFalse(ZspPathHelper.AreSameFolderPaths(s1, s2)); s1 = @"c:\folder1\folder2\folder3\folder4\.."; s2 = @"c:\folder1\folder2\folder3"; Assert.IsTrue(ZspPathHelper.AreSameFolderPaths(s1, s2)); s1 = @"c:\folder1\folder2\folder3\folder4\..\..\folder3"; s2 = @"c:\folder1\folder2\folder3"; Assert.IsTrue(ZspPathHelper.AreSameFolderPaths(s1, s2)); }
public void TestGeneral2() { var s1 = @"C:\Users\ukeim\Documents\Visual Studio 2008\Projects\Zeta Producer 9\Zeta Producer Main\Deploy\Origin\Enterprise\C-Allgaier\Windows\Packaging\Stationary\DEU\FirstStart\StandardProject"; var s2 = Path.GetFullPath(s1); Assert.AreEqual( @"C:\Users\ukeim\Documents\Visual Studio 2008\Projects\Zeta Producer 9\Zeta Producer Main\Deploy\Origin\Enterprise\C-Allgaier\Windows\Packaging\Stationary\DEU\FirstStart\StandardProject", s2); // -- s1 = @"c:\ablage\..\windows\notepad.exe"; s2 = Path.GetFullPath(s1); Assert.AreEqual(@"c:\windows\notepad.exe", s2); //-- s1 = @"lalala-123"; s2 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(@"lalala-123", s2); //-- s1 = @"lalala-123.txt"; s2 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(@"lalala-123", s2); //-- s1 = @"C:\Ablage\lalala-123.txt"; s2 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(@"lalala-123", s2); //-- s1 = @"\\nas001\data\folder\lalala-123.txt"; s2 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(@"lalala-123", s2); //-- s1 = @"c:\ablage\..\windows\notepad.exe"; s2 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(@"notepad", s2); //-- s1 = @"c:\ablage\..\windows\notepad.exe"; s2 = ZspPathHelper.GetExtension(s1); Assert.AreEqual(@".exe", s2); //-- //-- s1 = @"c:\ablage\..\windows\notepad.file.exe"; s2 = ZspPathHelper.GetExtension(s1); Assert.AreEqual(@".exe", s2); //-- s1 = @"c:\ablage\..\windows\notepad.exe"; s2 = ZspPathHelper.ChangeExtension(s1, @".com"); Assert.AreEqual(@"c:\ablage\..\windows\notepad.com", s2); // -- s1 = @"file.ext"; s2 = @"c:\ablage\path1\path2"; var s3 = @"c:\ablage\path1\path2\file.ext"; var s4 = ZspPathHelper.GetAbsolutePath(s1, s2); Assert.AreEqual(s3, s4); var s5 = s1.MakeAbsoluteTo(new DirectoryInfo(s2)); Assert.AreEqual(s3, s5); // -- s1 = @"c:\folder1\folder2\folder4\"; s2 = @"c:\folder1\folder2\folder3\file1.txt"; s3 = ZspPathHelper.GetRelativePath(s1, s2); s4 = @"..\folder3\file1.txt"; Assert.AreEqual(s3, s4); }
public void TestCompareWithFrameworkFunctions() { // -- var s1 = ZspPathHelper.GetFileNameFromFilePath(@"/suchen.html"); var s2 = Path.GetFileName(@"/suchen.html"); Assert.AreEqual(s1, s2); // -- s1 = ZspPathHelper.GetDirectoryPathNameFromFilePath(@"sitemap.xml"); s2 = Path.GetDirectoryName(@"sitemap.xml"); Assert.AreEqual(s1, s2); //s1 = ZspPathHelper.GetDirectoryPathNameFromFilePath(@""); //s2 = Path.GetDirectoryName(@""); //Assert.AreEqual(s1, s2); s1 = ZspPathHelper.GetDirectoryPathNameFromFilePath(@"c:\ablage\sitemap.xml"); s2 = Path.GetDirectoryName(@"c:\ablage\sitemap.xml"); Assert.AreEqual(s1, s2); s1 = ZspPathHelper.GetDirectoryPathNameFromFilePath(@"c:\ablage\"); s2 = Path.GetDirectoryName(@"c:\ablage\"); Assert.AreEqual(s1, s2); s1 = ZspPathHelper.GetDirectoryPathNameFromFilePath(@"c:\ablage"); s2 = Path.GetDirectoryName(@"c:\ablage"); Assert.AreEqual(s1, s2); s1 = ZspPathHelper.GetDirectoryPathNameFromFilePath(@"c:/ablage/sitemap.xml"); s2 = Path.GetDirectoryName(@"c:/ablage/sitemap.xml"); Assert.AreEqual(s1, s2); // -- s1 = @"c:\folder1\folder2\folder3\file1.txt"; var s3 = ZspPathHelper.GetFileNameFromFilePath(s1); var s4 = Path.GetFileName(s1); Assert.AreEqual(s3, s4); // -- s1 = @"c:\folder1\folder2\folder3\file1"; s3 = ZspPathHelper.GetFileNameFromFilePath(s1); s4 = Path.GetFileName(s1); Assert.AreEqual(s3, s4); // -- s1 = @"c:\folder1\folder2\folder3\file1."; s3 = ZspPathHelper.GetFileNameFromFilePath(s1); s4 = Path.GetFileName(s1); Assert.AreEqual(s3, s4); // -- s1 = @"file1.txt"; s3 = ZspPathHelper.GetFileNameFromFilePath(s1); s4 = Path.GetFileName(s1); Assert.AreEqual(s3, s4); // -- s1 = @"file1"; s3 = ZspPathHelper.GetFileNameFromFilePath(s1); s4 = Path.GetFileName(s1); Assert.AreEqual(s3, s4); // -- s1 = @"file1."; s3 = ZspPathHelper.GetFileNameFromFilePath(s1); s4 = Path.GetFileName(s1); Assert.AreEqual(s3, s4); // -- s1 = @"c:\folder1\folder2\folder3\file1.txt"; s3 = Path.GetFileNameWithoutExtension(s1); s4 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(s3, s4); // -- s1 = @"c:\folder1\folder2\folder3\file1"; s3 = Path.GetFileNameWithoutExtension(s1); s4 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(s3, s4); // -- s1 = @"c:\folder1\folder2\folder3\file1."; s3 = Path.GetFileNameWithoutExtension(s1); s4 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(s3, s4); // -- s1 = @"file1.txt"; s3 = Path.GetFileNameWithoutExtension(s1); s4 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(s3, s4); // -- s1 = @"file1"; s3 = Path.GetFileNameWithoutExtension(s1); s4 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(s3, s4); // -- s1 = @"file1."; s3 = Path.GetFileNameWithoutExtension(s1); s4 = Path.GetFileNameWithoutExtension(s1); Assert.AreEqual(s3, s4); }
public void TestGeneral() { // Ordner mit Punkt am Ende. var dir = $@"C:\Ablage\{Guid.NewGuid():N}."; Assert.IsFalse(new DirectoryInfo(dir).Exists); new DirectoryInfo(dir).CheckCreate(); Assert.IsTrue(new DirectoryInfo(dir).Exists); new DirectoryInfo(dir).Delete(true); Assert.IsFalse(new DirectoryInfo(dir).Exists); //Assert.IsTrue(new DirectoryInfo(Path.GetTempPath()).CreationTime>DateTime.MinValue); //Assert.IsTrue(new DirectoryInfo(Path.GetTempPath()).Exists); //Assert.IsFalse(new DirectoryInfo(@"C:\Ablage\doesnotexistjdlkfjsdlkfj").Exists); //Assert.IsTrue(new DirectoryInfo(Path.GetTempPath()).Exists); //Assert.IsFalse(new DirectoryInfo(@"C:\Ablage\doesnotexistjdlkfjsdlkfj2").Exists); //Assert.IsFalse(new DirectoryInfo(@"\\zetac11\C$\Ablage").Exists); //Assert.IsFalse(new DirectoryInfo(@"\\zetac11\C$\Ablage\doesnotexistjdlkfjsdlkfj2").Exists); const string s1 = @"C:\Users\Chris\Documents\Development\ADC\InterStore.NET\Visual Studio 2008\6.4.2\Zeta Resource Editor"; const string s2 = @"C:\Users\Chris\Documents\Development\ADC\InterStore.NET\Visual Studio 2008\6.4.2\Web\central\Controls\App_LocalResources\ItemSearch"; var s3 = ZspPathHelper.GetRelativePath(s1, s2); Assert.AreEqual(s3, @"..\Web\central\Controls\App_LocalResources\ItemSearch"); var ext = ZspPathHelper.GetExtension(s3); Assert.AreEqual(ext ?? string.Empty, string.Empty); ext = ZspPathHelper.GetExtension(@"C:\Ablage\Uwe.txt"); Assert.AreEqual(ext, @".txt"); const string path = @"C:\Ablage\Test"; Assert.AreEqual( new DirectoryInfo(path).Name, new DirectoryInfo(path).Name); Assert.AreEqual( new DirectoryInfo(path).FullName, new DirectoryInfo(path).FullName); const string filePath = @"C:\Ablage\Test\file.txt"; var fn1 = new FileInfo(filePath).Directory?.FullName; var fn2 = new FileInfo(filePath).Directory.FullName; var fn1A = new FileInfo(filePath).DirectoryName; var fn2A = new FileInfo(filePath).DirectoryName; Assert.AreEqual(fn1, fn2); Assert.AreEqual(fn1A, fn2A); var fn = new DirectoryInfo(@"\\zetac11\C$\Ablage\doesnotexistjdlkfjsdlkfj2").Parent.FullName; Assert.AreEqual(fn, @"\\zetac11\C$\Ablage"); fn = new DirectoryInfo(@"\\zetac11\C$\Ablage\doesnotexistjdlkfjsdlkfj2\").Parent.FullName; Assert.AreEqual(fn, @"\\zetac11\C$\Ablage"); }
public void TestGeneral() { var tempFolder = Environment.ExpandEnvironmentVariables("%temp%"); Assert.IsTrue(Directory.Exists(tempFolder)); var tempPath = ZspPathHelper.Combine(tempFolder, "ZspTest"); try { Directory.CreateDirectory(tempPath); Assert.IsTrue(Directory.Exists(tempPath)); var filePath = ZspPathHelper.Combine(tempPath, "text.zsp"); using (var textStream = new StreamWriter(new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))) { textStream.WriteLine("Zeta Short Paths Extended testing..."); textStream.Flush(); } Assert.IsTrue(File.Exists(filePath)); var m = ZspIOHelper.GetFileLength(filePath); Assert.IsTrue(m > 0); Assert.IsTrue(m == new FileInfo(filePath).Length); Assert.IsTrue(File.Exists(@"c:\Windows\notepad.exe")); Assert.IsFalse(File.Exists(@"c:\dslfsdjklfhsd\kjsaklfjd.exe")); Assert.IsFalse(File.Exists(@"c:\ablage")); Assert.IsFalse(Directory.Exists(@"c:\Windows\notepad.exe")); Assert.IsTrue(Directory.Exists(@"c:\Windows")); Assert.IsTrue(Directory.Exists(@"c:\Windows\")); Assert.IsFalse(Directory.Exists(@"c:\fkjhskfsdhfjkhsdjkfhsdkjfh")); Assert.IsFalse(Directory.Exists(@"c:\fkjhskfsdhfjkhsdjkfhsdkjfh\")); // -- AssertOwn.DoesNotThrow(() => File.SetLastWriteTime(filePath, new DateTime(1986, 1, 1))); AssertOwn.DoesNotThrow(() => File.SetLastAccessTime(filePath, new DateTime(1987, 1, 1))); AssertOwn.DoesNotThrow(() => File.SetCreationTime(filePath, new DateTime(1988, 1, 1))); AssertOwn.DoesNotThrow(() => Directory.SetLastWriteTime(tempPath, new DateTime(1986, 1, 1))); AssertOwn.DoesNotThrow(() => Directory.SetLastAccessTime(tempPath, new DateTime(1987, 1, 1))); AssertOwn.DoesNotThrow(() => Directory.SetCreationTime(tempPath, new DateTime(1988, 1, 1))); var anotherFile = ZspPathHelper.Combine(tempPath, "test2.zsp"); File.WriteAllText(anotherFile, @"הצ�."); Assert.IsTrue(File.Exists(anotherFile)); var time = File.GetLastWriteTime(filePath); Assert.IsTrue(time > DateTime.MinValue); var l = ZspIOHelper.GetFileLength(anotherFile); Assert.IsTrue(l > 0); } finally { Directory.Delete(tempPath, true); } }