FileInfo.Replace Methode

Definitie

Vervangt de inhoud van een opgegeven bestand door het bestand dat wordt beschreven door het huidige FileInfo object, waarbij het oorspronkelijke bestand wordt verwijderd en een back-up van het vervangen bestand wordt gemaakt.

Overloads

Name Description
Replace(String, String)

Vervangt de inhoud van een opgegeven bestand door het bestand dat wordt beschreven door het huidige FileInfo object, waarbij het oorspronkelijke bestand wordt verwijderd en een back-up van het vervangen bestand wordt gemaakt.

Replace(String, String, Boolean)

Hiermee vervangt u de inhoud van een opgegeven bestand door het bestand dat wordt beschreven door het huidige FileInfo object, verwijdert u het oorspronkelijke bestand en maakt u een back-up van het vervangen bestand. Hiermee geeft u ook op of samenvoegfouten moeten worden genegeerd.

Opmerkingen

Gebruik de Replace methoden wanneer u snel een bestand wilt vervangen door de inhoud van het bestand dat wordt beschreven door het huidige FileInfo object.

Replace(String, String)

Vervangt de inhoud van een opgegeven bestand door het bestand dat wordt beschreven door het huidige FileInfo object, waarbij het oorspronkelijke bestand wordt verwijderd en een back-up van het vervangen bestand wordt gemaakt.

public:
 System::IO::FileInfo ^ Replace(System::String ^ destinationFileName, System::String ^ destinationBackupFileName);
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName);
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName);
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Replace : string * string -> System.IO.FileInfo
member this.Replace : string * string -> System.IO.FileInfo
Public Function Replace (destinationFileName As String, destinationBackupFileName As String) As FileInfo

Parameters

destinationFileName
String

De naam van een bestand dat moet worden vervangen door het huidige bestand.

destinationBackupFileName
String

De naam van een bestand waarmee een back-up van het bestand moet worden gemaakt dat door de destinationFileName parameter wordt beschreven.

Retouren

Een FileInfo object dat informatie bevat over het bestand dat door de destinationFileName parameter wordt beschreven.

Kenmerken

Uitzonderingen

Het pad dat door de destinationFileName parameter wordt beschreven, is niet van een juridische vorm.

– of –

Het pad dat door de destinationBackupFileName parameter wordt beschreven, is niet van een juridische vorm.

De destinationFileName parameter is null.

Het bestand dat door het huidige FileInfo object wordt beschreven, is niet gevonden.

– of –

Het bestand dat door de destinationFileName parameter wordt beschreven, is niet gevonden.

Het huidige besturingssysteem is niet Microsoft Windows NT of hoger.

Voorbeelden

In het volgende voorbeeld wordt de Replace methode gebruikt om een bestand te vervangen door een ander bestand en een back-up van het vervangen bestand te maken.

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
Imports System.IO

Module FileExample

    Sub 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.

            Dim originalFile As String = "test.xml"
            Dim fileToReplace As String = "test2.xml"
            Dim backUpOfFileToReplace As String = "test2.xml.bak"

            If (File.Exists(originalFile) And (File.Exists(fileToReplace))) Then
                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)
            End If
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try

        Console.ReadLine()

    End Sub

    ' Move a file into another file, delete the original, and create a backup of the replaced file.
    Sub ReplaceFile(ByVal fileToMoveAndDelete As String, ByVal fileToReplace As String, ByVal backupOfFileToReplace As String)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(fileToMoveAndDelete)

        ' Replace the file.
        fInfo.Replace(fileToReplace, backupOfFileToReplace, False)

    End Sub
End Module

' Move the contents of test.txt into test2.txt, delete test.txt, and 
' create a backup of test2.txt.
' Done

Opmerkingen

De Replace methode vervangt de inhoud van een opgegeven bestand door de inhoud van het bestand dat door het huidige FileInfo object wordt beschreven. Er wordt ook een back-up gemaakt van het bestand dat is vervangen. Ten slotte wordt een nieuw FileInfo object geretourneerd dat het overschreven bestand beschrijft.

Geef null de destinationBackupFileName parameter door als u geen back-up wilt maken van het bestand dat wordt vervangen.

Van toepassing op

Replace(String, String, Boolean)

Hiermee vervangt u de inhoud van een opgegeven bestand door het bestand dat wordt beschreven door het huidige FileInfo object, verwijdert u het oorspronkelijke bestand en maakt u een back-up van het vervangen bestand. Hiermee geeft u ook op of samenvoegfouten moeten worden genegeerd.

public:
 System::IO::FileInfo ^ Replace(System::String ^ destinationFileName, System::String ^ destinationBackupFileName, bool ignoreMetadataErrors);
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Replace : string * string * bool -> System.IO.FileInfo
member this.Replace : string * string * bool -> System.IO.FileInfo
Public Function Replace (destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean) As FileInfo

Parameters

destinationFileName
String

De naam van een bestand dat moet worden vervangen door het huidige bestand.

destinationBackupFileName
String

De naam van een bestand waarmee een back-up van het bestand moet worden gemaakt dat door de destinationFileName parameter wordt beschreven.

ignoreMetadataErrors
Boolean

true samenvoegfouten (zoals kenmerken en ACL's) van het vervangen bestand naar het vervangen bestand te negeren; anders false.

Retouren

Een FileInfo object dat informatie bevat over het bestand dat door de destinationFileName parameter wordt beschreven.

Kenmerken

Uitzonderingen

Het pad dat door de destinationFileName parameter wordt beschreven, is niet van een juridische vorm.

– of –

Het pad dat door de destinationBackupFileName parameter wordt beschreven, is niet van een juridische vorm.

De destinationFileName parameter is null.

Het bestand dat door het huidige FileInfo object wordt beschreven, is niet gevonden.

– of –

Het bestand dat door de destinationFileName parameter wordt beschreven, is niet gevonden.

Het huidige besturingssysteem is niet Microsoft Windows NT of hoger.

Voorbeelden

In het volgende voorbeeld wordt de Replace methode gebruikt om een bestand te vervangen door een ander bestand en een back-up van het vervangen bestand te maken.

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
Imports System.IO

Module FileExample

    Sub 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.

            Dim originalFile As String = "test.xml"
            Dim fileToReplace As String = "test2.xml"
            Dim backUpOfFileToReplace As String = "test2.xml.bak"

            If (File.Exists(originalFile) And (File.Exists(fileToReplace))) Then
                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)
            End If
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try

        Console.ReadLine()

    End Sub

    ' Move a file into another file, delete the original, and create a backup of the replaced file.
    Sub ReplaceFile(ByVal fileToMoveAndDelete As String, ByVal fileToReplace As String, ByVal backupOfFileToReplace As String)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(fileToMoveAndDelete)

        ' Replace the file.
        fInfo.Replace(fileToReplace, backupOfFileToReplace, False)

    End Sub
End Module

' Move the contents of test.txt into test2.txt, delete test.txt, and 
' create a backup of test2.txt.
' Done

Opmerkingen

De Replace methode vervangt de inhoud van een opgegeven bestand door de inhoud van het bestand dat door het huidige FileInfo object wordt beschreven. Er wordt ook een back-up gemaakt van het bestand dat is vervangen. Ten slotte wordt een nieuw FileInfo object geretourneerd dat het overschreven bestand beschrijft.

Geef null de destinationBackupFileName parameter door als u geen back-up wilt maken van het bestand dat wordt vervangen.

Van toepassing op