Replace File Content

In this chapter you will learn:

  1. How to replace the content of a text file

File.Replace Replaces contents of file

using System;//from   j  a v  a  2 s  . c  om
using System.IO;

class FileExample
{
    public static void Main()
    {
        string OriginalFile = "test.xml";
        string FileToReplace = "test2.xml";
        ReplaceFile(OriginalFile, FileToReplace, "test3.xml");
    }
    public static void ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace)
    {
        File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);

    }
}

Next chapter...

What you will learn in the next chapter:

  1. Get to know BinaryReader
  2. Example to use BinaryReader to read data
Home » C# Tutorial » Stream
Stream classes
Text File Read
Text File write
Text File Create
Text File Append
Replace File Content
BinaryReader
BinaryWriter
FileStream Create
FileStream byte read and write
BufferedStream
Compare File
File Copy
File Copy with FileStream
MemoryStream
Object Serialization
String Writer