File.Move不工作,没有错误

时间:2015-04-29 17:11:39

标签: c# .net

我在尝试在服务器上移动平面HTML文件时遇到了一些问题。

简而言之,该过程表明它应该采用当前在新路径中的任何文件(如果有的话)并将其移动到备份文件夹。然后在旧路径中获取文件并将其移动到新路径。然后它检查它是否成功(即新路径是否存在),如果不存在,则替换备份。我已经粘贴了下面的方法以供您观看。

    public bool MoveContent(string oldPath, string newPath, string backupDirectoryPath, out string backupPath)
    {
        backupPath = String.Empty;
        var oldFilePath = HttpContext.Current.Server.MapPath(oldPath);
        var newFilePath = HttpContext.Current.Server.MapPath(newPath);
        var newDirectory = Path.GetDirectoryName(newFilePath);

        // If the file we're moving doesn't exist, fail.
        if (!File.Exists(oldFilePath))
            throw new InvalidPathException(oldFilePath);

        // If no destination is found, fail.
        if (string.IsNullOrWhiteSpace(newDirectory))
            throw new InvalidPathException(newFilePath);

        if (!Directory.Exists(newDirectory))
            Directory.CreateDirectory(newDirectory);

        var backupPhysicalPath = String.Empty;
        // If there is a file in our destination, back that one up.
        if (File.Exists(newFilePath) && !String.IsNullOrWhiteSpace(backupDirectoryPath))
        {
            var backupFilePath = HttpContext.Current.Server.MapPath(backupDirectoryPath);
            var backupDirectory = Path.GetDirectoryName(backupFilePath);

            // If the backup destination doesn't exist, fail.
            if(string.IsNullOrWhiteSpace(backupDirectory))
                throw new InvalidPathException(backupDirectory);

            if(!Directory.Exists(backupDirectory))
                Directory.CreateDirectory(backupDirectory);

            var fileName = Path.GetFileNameWithoutExtension(newFilePath);
            var currentDateTime = DateTime.Now.ToString(FileHelpers.TempFileDateFormat);
            var fileExtension = Path.GetExtension(newFilePath);
            // Example Result: hardware-2015-01-30-08-35-26-475.html
            backupPath = backupDirectoryPath.Replace(fileName, fileName + "-" + currentDateTime);
            backupPhysicalPath = String.Format("{0}\\{1}-{2}{3}", backupDirectory, fileName, currentDateTime, fileExtension);

            // If there is already a file in our backup destination, fail.
            if (File.Exists(backupPhysicalPath))
                throw new InvalidPathException(backupPhysicalPath);

            // Backup the file that currently exists in our new destination.
            File.Move(newFilePath, backupPhysicalPath);
        }

        // Move our file to the new destination.
        File.Move(oldFilePath, newFilePath);

        // Return false if the new file doesn't exist.
        if (!File.Exists(newFilePath))
        {
            // If we made a backup, return the backup to the original loction, since there's nothing in the destination.
            if (!String.IsNullOrWhiteSpace(backupPhysicalPath) && File.Exists(backupPhysicalPath))
            {
                File.Move(backupPhysicalPath, newFilePath);
            }

            throw new Exception(String.Format("Failed to move content. OldPath: '{0}'; NewPath: '{1}'; BackupPath: '{2}'", oldFilePath, newFilePath, backupPhysicalPath));
        }

        return true;
    }

以下是传入参数的示例:

oldPath: "/client/content/en/unpublished/Anpan.html"
newPath: "/client/content/en/Anpan.html"
backupDirectoryPath: "/client/content/en/backups/Anpan.html"

我遇到的问题是有时备份文件会被创建(它会从newpath移动到backuppath),但它不会从oldpath移动到newpath。

我一直无法真正重现这个问题,因为它很少发生并且没有任何异常被抛出,但症状存在(我可以在客户端报告问题时看到文件系统上的文件)并且已报告多个倍。

我在它周围记录并将整个方法包装在try / catch中。它永远不会意外失败(除非我专门抛出InvalidPathException)。发生时,我的日志中没有任何内容。

任何人都可以帮我诊断这个问题,或者告诉我,如果我的方法中出现了一些非常错误的问题会导致问题吗?

非常感谢!

0 个答案:

没有答案