System.IO.FileInfo.Replace 方法 (String, String)

方法描述

使用当前 FileInfo 对象所描述的文件替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。

语法定义(C# System.IO.FileInfo.Replace 方法 (String, String) 的用法)

[ComVisibleAttribute(false)]
public FileInfo Replace(
	string destinationFileName,
	string destinationBackupFileName
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
destinationFileName System-String 要替换为当前文件的文件的名称。
destinationBackupFileName System-String 文件的名称,该文件用于创建 destFileName 参数所描述的文件的备份。
返回值 System.IO.FileInfo 一个 FileInfo 对象,该对象封装有关 destFileName 参数所描述的文件的信息。

提示和注释

Replace 方法用当前 FileInfo 对象所描述的文件的内容替换指定文件的内容。 它还创建被替换文件的备份。 最后,它返回一个新的描述被覆盖文件的 FileInfo 对象。

警告

如果 destFileName 为只读并且不会引发异常,则该方法可用于 Windows 2000 环境。 在尝试替换目标文件之前,请使用 IsReadOnly 属性检查该目标文件是否为只读。

如果不需要创建正在被替换的文件的备份,请将 null 传递给 destBackupFileName 参数。

System.IO.FileInfo.Replace 方法 (String, String)例子

下面的示例使用 Replace 方法将一个文件替换为另一个文件并创建被替换文件的备份。

using System;
using System.IO;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                // originalFile and fileToReplace must contain the path to files that already exist in the  
                // file system. backUpOfFileToReplace is created during the execution of the Replace method.

                string originalFile  = "test.txt";
                string fileToReplace = "test2.txt";
                string backUpOfFileToReplace = "test2.txt.bak";

                if (File.Exists(originalFile) && (File.Exists(fileToReplace)))
                {
                    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");
                }
                else
                {
                    Console.WriteLine("Either the file {0} or {1} doesn't " + "exist.", originalFile, fileToReplace);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            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)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(fileToMoveAndDelete);

            // replace the file.
            fInfo.Replace(fileToReplace, backupOfFileToReplace, false);
        }
    }
}
//Move the contents of test.txt into test2.txt, delete test.txt, and 
//create a backup of test2.txt.
//Done

异常

异常 异常描述
ArgumentException
  • destFileName 参数描述的路径不是合法的格式。
  • destBackupFileName 参数描述的路径不是合法的格式。
ArgumentNullException destFileName 参数为 null。
FileNotFoundException
  • 未能找到当前 FileInfo 对象所描述的文件。
  • 未能找到 destinationFileName 参数所描述的文件。
PlatformNotSupportedException 当前操作系统不是 Microsoft Windows NT 或更高版本。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。