File.Replace(String,String,String)是一个内置的File类方法,用于将指定目标文件的内容替换为源文件的内容,然后删除源文件并创建替换文件的备份。
句法:
public static void Replace (string sourceFileName, string destinationFileName, string destinationBackupFileName);
参数:此函数接受三个参数,如下所示:
- sourceFileName: This is the specified source file.
- destinationFileName: This is the specified destination file whose contents get replaced with the contents of the source file.
- destinationBackupFileName: This file contains the backup of replaced destination file’s content.
例外情况:
- ArgumentException: destinationFileName参数描述的路径不是合法形式。或destinationBackupFileName参数描述的路径不是合法形式。
- ArgumentNullException: destinationFileName参数为null。
- DriveNotFoundException:指定了无效的驱动器。
- FileNotFoundException:找不到由当前FileInfo对象描述的文件。或找不到由destinationBackupFileName参数描述的文件。
- IOException:打开文件时发生I / O错误。或sourceFileName和destinationFileName参数指定相同的文件。
- PathTooLongException:指定的路径,文件名或两者都超过系统定义的最大长度。
- PlatformNotSupportedException:操作系统是Windows 98 Second Edition或更早版本,文件系统不是NTFS。
- UnauthorizedAccessException: sourceFileName或destinationFileName参数指定一个只读文件。或当前平台不支持此操作。 OR源或目标参数指定目录而不是文件。或呼叫者没有所需的权限。
下面是说明File.Replace(String,String,String)方法的程序。
程序1:在运行以下代码之前,已创建了三个文件,其中源文件为file1.txt ,目标文件为file2.txt ,备份文件为file3.txt 。这些文件的内容如下所示-
CSharp
// C# program to illustrate the usage// of File.Replace(String, String, String) method// Using System and System.IO namespacesusing System;using System.IO;class GFG {public static void Main(){// Specifying 3 filesstring sourceFileName = "file1.txt";string destinationFileName = "file2.txt";string destinationBackupFileName = "file3.txt";// Calling the Replace() functionFile.Replace(sourceFileName, destinationFileName,destinationBackupFileName);Console.WriteLine("Replacement process has been done.");}}
C#
// C# program to illustrate the usage
// of File.Replace(String, String, String) method
// Using System and System.IO namespaces
using System;
using System.IO;
class GFG {
public static void Main()
{
// Specifying 2 files
string sourceFileName = "file1.txt";
string destinationFileName = "file2.txt";
// Calling the Replace() function with
// null parameter inplace of backup file because
// we do not want to keep backup of the
// replaced file.
File.Replace(sourceFileName, destinationFileName, null);
Console.WriteLine("Replacement process has been done.");
}
}
输出:
Replacement process has been done.
运行以上代码后,将显示以上输出,将源文件删除,其余两个文件的内容如下所示:
程序2:在运行下面的代码之前,已经创建了两个文件,其中源文件为file1.txt ,目标文件为file2.txt,并且没有备份文件,因为我们不想保留替换文件的备份。这些文件的内容如下所示-
C#
// C# program to illustrate the usage
// of File.Replace(String, String, String) method
// Using System and System.IO namespaces
using System;
using System.IO;
class GFG {
public static void Main()
{
// Specifying 2 files
string sourceFileName = "file1.txt";
string destinationFileName = "file2.txt";
// Calling the Replace() function with
// null parameter inplace of backup file because
// we do not want to keep backup of the
// replaced file.
File.Replace(sourceFileName, destinationFileName, null);
Console.WriteLine("Replacement process has been done.");
}
}
输出:
Replacement process has been done.
运行上述代码后,将显示以上输出,删除源文件,并在下面显示目标文件的内容-