本文整理匯總了C#中System.IO.FileInfo.Replace方法的典型用法代碼示例。如果您正苦於以下問題:C# FileInfo.Replace方法的具體用法?C# FileInfo.Replace怎麽用?C# FileInfo.Replace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IO.FileInfo
的用法示例。
在下文中一共展示了FileInfo.Replace方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
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