예제 #1
0
    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
                }
            }
        }
    }
예제 #2
0
    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
            }
        }
    }
예제 #3
0
        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);
        }