Difference between File.Replace and (File.Delete+File.Move) in C#

时间:2022-07-18 13:21:43

Today I ran into a strange problem: Since a year and several versions / tests of the application the following code has been used successfully to replace a file with an other.

今天我遇到了一个奇怪的问题:由于应用程序的一年和多个版本/测试,以下代码已成功用于替换另一个文件。

File.Replace(path + ".tmp", path, null);

This has worked locally and also with UNC paths (network shares). But today I got the following error when I used this code to replace a file on a UNC path (local still works):

这在本地以及UNC路径(网络共享)中都有效。但是今天我使用此代码替换UNC路径上的文件时出现以下错误(本地仍可正常工作):

The process cannot access the file because it is being used by another process

该进程无法访问该文件,因为该文件正由另一个进程使用

When I use the following code instead of the above, it works:

当我使用以下代码而不是上述代码时,它可以工作:

File.Delete(path);
File.Move(path + ".tmp", path);

So my questions:

所以我的问题:

  • What is the difference between the two code snippets?
  • 这两个代码片段有什么区别?
  • Could it be that Microsoft have changed the way 'File.Replace' works?
  • 可能是微软改变了'File.Replace'的工作方式吗?

I'm using .Net Framework 4.0 with Visual Studio 2010.

我在Visual Studio 2010中使用.Net Framework 4.0。

Thanks in advance.

提前致谢。

4 个解决方案

#1


5  

According to MSDN on File.Replace

根据MSDN对File.Replace的说法

File.Replace will throw an exception when...

File.Replace会在...时抛出异常

  • the destination file is missing.
  • 目标文件丢失。
  • source and destination are on different volumes
  • 源和目标位于不同的卷上

Which File.Delete, File.Move won't.

哪个File.Delete,File.Move不会。

#2


4  

Here's the MSDN article on File.Replace()

这是关于File.Replace()的MSDN文章

Creating a backup of the original appears to be the difference.

创建原始备份似乎是不同的。

#3


1  

The article linked by Phil Murray says File.Replace replaces the contents of the file. Perhaps it is trying to open the file with write access?

Phil Murray链接的文章称File.Replace替换了文件的内容。也许它试图用写访问权限打开文件?

#4


0  

using System;
using System.IO;

namespace FileSystemExample
{

class FileExample
{

    public static void Main()
    {
        try
        {

            string OriginalFile = path+"test.tmp";
            string FileToReplace = path+"test2.tmp";
            string BackUpOfFileToReplace = path+"test2.tmp.bac";


            Console.WriteLine("Move the contents of " + OriginalFile + " into " + FileToReplace + ", delete " + OriginalFile +
                               ", and create a backup of " + FileToReplace + ".");

            // Replace the file.
            ReplaceFile(@OriginalFile, @FileToReplace, @BackUpOfFileToReplace);

            Console.WriteLine("Done");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        Console.ReadLine();
    }

    // Move a file into another file, delete the original, and create a backup of the replaced file.
    public static void ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace)
    {
        File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);

    }
}

}

}

#1


5  

According to MSDN on File.Replace

根据MSDN对File.Replace的说法

File.Replace will throw an exception when...

File.Replace会在...时抛出异常

  • the destination file is missing.
  • 目标文件丢失。
  • source and destination are on different volumes
  • 源和目标位于不同的卷上

Which File.Delete, File.Move won't.

哪个File.Delete,File.Move不会。

#2


4  

Here's the MSDN article on File.Replace()

这是关于File.Replace()的MSDN文章

Creating a backup of the original appears to be the difference.

创建原始备份似乎是不同的。

#3


1  

The article linked by Phil Murray says File.Replace replaces the contents of the file. Perhaps it is trying to open the file with write access?

Phil Murray链接的文章称File.Replace替换了文件的内容。也许它试图用写访问权限打开文件?

#4


0  

using System;
using System.IO;

namespace FileSystemExample
{

class FileExample
{

    public static void Main()
    {
        try
        {

            string OriginalFile = path+"test.tmp";
            string FileToReplace = path+"test2.tmp";
            string BackUpOfFileToReplace = path+"test2.tmp.bac";


            Console.WriteLine("Move the contents of " + OriginalFile + " into " + FileToReplace + ", delete " + OriginalFile +
                               ", and create a backup of " + FileToReplace + ".");

            // Replace the file.
            ReplaceFile(@OriginalFile, @FileToReplace, @BackUpOfFileToReplace);

            Console.WriteLine("Done");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        Console.ReadLine();
    }

    // Move a file into another file, delete the original, and create a backup of the replaced file.
    public static void ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace)
    {
        File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);

    }
}

}

}