Click here to Skip to main content
11,648,094 members (72,379 online)
Click here to Skip to main content

String Replace and File Handling library

, 8 Jan 2008 CPOL 18.7K 531 30
Rate this:
Please Sign up or sign in to vote.
Library which contains code for replacing some of the Text in files based on conditions and handling the files by reading and writing strings from or to the file by recursively browsing the folder.


The article is mainly for beginners of .NET programming, who will be in need of creating some UI Tools/Console application for replacing some of the Text in various files based on their needs. I have written a TextReplaceIOLibrary, includes DirectoryHandlerand a StringReplaceHelperclass which will be helpful for the purpose of StringReplacementin a text file based on the user conditions by recursively browsing through the folder. I also created a simple StringReplaceToolsample using the DirectoryHandlerand StringReplaceHelperclass, where anyone can create a Search and Replace Tool for their needs with very little code. I didn't get more time to spend on the sample, to make it better, and illustrate all the functionality. However, if anyone needs a specific sample for explaining any functionality, please email me.


During my earlier development on Custom controls, and Website Development, I came across many situations where I was in need of replacing the version numbers during each build performed. So, I thought of having a library which will help to handle such scenarios (However, I haven't used or tested this library in more scenarios now).

Using the Code

About TextReplaceIO Namespace

The namespace consists of two classes, one for handling the operations on files and folder and another for replacing strings in various conditions.


This class is used for handling the following operations on files and folders:

  1. Reading text files as string. The below method is used for this purpose:

    /// <summary>
    /// Read the contents of the specified file and return it as a string.
    /// </summary>
    /// <remarks>
    /// If you try to read unsupported file types, and then save the 
    /// file, some files might get corrupted. so make sure to only  
    ///open and write supported files.
    /// </remarks>
    /// <param name=""nextFile"">File that needs to be read</param>
    public string ReadFileContents(FileInfo nextFile)
        // updating the variable about the file that is reading.
        this.processingFileInFo = nextFile;
        FileStream fs = new FileStream(nextFile.FullName, 
    			FileMode.OpenOrCreate, FileAccess.ReadWrite);
        StreamReader sr = new StreamReader(fs);
        String fileString = sr.ReadToEnd();
        return fileString;
  2. Writing stringto text files. The following method is used for this purpose:

    /// <summary>
    /// Write the string contents to a text file.
    /// </summary>
    /// <param name=""strWrite"">string to be written on the file</param>
    /// <param name=""filePath"">Path in which the string has to be written as a file
    /// </param>
    /// <param name=""fileMode""><see cref="FileMode"" /></param>
    public void WriteContentsToFile
    	(string strWrite, string filePath, FileMode fileMode)
        String newFileString = strWrite;
        FileStream fs = new FileStream(filePath, fileMode, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
  3. Populate a treeviewwith System drives and load sub nodes on demand. (Actually, I got some core ideas from some other article, but am not able to find it now.). In the DirectoryHandler class, you have to use CreateTreeViewDirectory(TreeView treeView) for loading the drives into the TreeNode. A special folder Desktop type is added in the list, as it is a commonly used special folder. You need to call the following method in the BeforeExpand event of 'Treeview' for loading the treeviewnodes on demand. LoadDirectoryinTreeViewOnDemand(TreeNode parentNode) method is added in the DirectoryHandleras many developers like to do their file/folder operations by selecting from a treeview.

  4. Parse through all files and folders recursively based on filter conditions, and get the files using the ManipulateFilteredFile event.
    The following methods are used for this:

    /// <summary>
    /// Parse through all the folders in the specified directory 
    /// </summary>
    /// <param name=""dirInfo"">Provide the directory which needs to be parsed</param>
    /// <param name=""recursive"">Setting this to 'True' 
    /// will parse through all the subfolders in the directory.</param>
    public void ParseFolder(DirectoryInfo dirInfo, bool recursive)
        DirectoryInfo[] subDirs;
        FileInfo[] files;
        //Getting filelist of parent directory
        foreach (DirectoryInfo directory in dirInfo.GetDirectories())
            if (directory != null && directory.Exists)
                this.dirCount += 1;
                subDirs = directory.GetDirectories();
                if (recursive && subDirs.Length != 0)
                    //Getting file list of SubDirectories.
                    ParseSubFolder(directory, true);
                else if (recursive && files.Length > 0)
                    //Getting file list of SubDirectories.
    /// <summary>
    /// Parse through the contents of each folders
    /// </summary>
    /// <param name=""Parent""></param>
    protected void ListContentsOfFolder(DirectoryInfo Parent)
        foreach (FileInfo NextFile in Parent.GetFiles())
            this.fileCount += 1;
            if (this.FilteredFileTypes == null&& this.FilterFileNames==null)
                this.filterFileCount += 1;
                bool filteredFile = CheckForFilter(NextFile);
                if (filteredFile)
                    this.filterFileCount += 1;
    /// <summary>
    /// Raises the ManipulateFilteredFile event.
    /// </summary>
    /// <param name=""NextFile"">FileInfo</param>
    public virtual void OnManipulateFilteredFile(FileInfo NextFile)
        ManipulateFilteredFile(this, NextFile);        
  5. The ChangeEvents method is used for creating log files if a folder or file is changed, deleted or modified. Set the LogFileName property, in which the log files need to be written.

    /// <summary>
    /// Updates if there are any changes in the files in the  specified directory. 
    /// The changes will be appended to the file, if you set the LogFileName Property.
    /// </summary>
    /// <remarks>The User needs to create/clean the log file before he uses these 
    /// events based on the needs as the contents are only appended using this approach
    /// </remarks>
    /// <param name=""fsw"">The Folder that needs to be watched for changes</param>
    public void ChangeEvents(FileSystemWatcher fsw, bool enableRaisingEvents)
        fsw.Changed += new FileSystemEventHandler(fsw_Changed);
        fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
        fsw.Created += new FileSystemEventHandler(fsw_Created);
        fsw.Renamed += new RenamedEventHandler(fsw_Renamed);
        fsw.EnableRaisingEvents = true;
    void fsw_Renamed(object sender, RenamedEventArgs e)
        WriteContentsToFile(String.Format("\r\nRenamed {0} to {1}", 
    		e.OldName, e.Name), this.LogFileName, FileMode.Append);


This class is used for replacing Stringbased on the following conditions:

  1. Find a specific stringand replace it with another string
  2. Find a specific stringand replace it with another string, if the strings are in between a specified block
  3. Replace entire stringwithin a specified string
  4. Replace the entire stringwithin a specified stringif the strings are in between a specified block
  5. Replace a stringif and only if it is found before or after a specific string

All the above conditions, except (4) are implemented in ReplaceStringWithCondition method (i.e., a function call will be done in this method, in which based on the parameters, the ReplaceStringWithConditionfunction will be executed) and (4) is implemented in the ReplaceStringBetweenWithCondition method. For now, I have used two different types of logic to get the strings that need to be searched for replace conditions in each of the above mentioned methods. By further modification, this can be further simplified. I will try to do it later if I get some time to spend on this:

  1. In ReplaceStringWithCondition, I used the inbuilt string.split method to split the entire stringbased upon the StartWithor EndsWith stringas the Tokenizer, and then after modifying the string, I will merge the Tokenizers also along with the strings.
  2. In ReplaceStringBetweenWithCondition to get the stringthat needs to be checked for replacing some of the contents in it, I have split by finding the index of the provided stringblock.

About StringReplaceSample

The sample explains almost all the functionalities of the TextReplaceIOlibrary, which is mentioned in the TextReplaceIOsection. You can use only some of the methods in your console application or any other application based on your need. Use the sample in the following way. See the code inside the sample for you to get more ideas on the implementations.


Select the File/Folder, from the Treeview, in which you want some of the strings of some files inside the directory that need to be replaced.



Provide the Filter condition of files that needs to be searched either by giving FileNameor by Filetypeor both.

File Filter condition


Then based upon the condition to replace, fill the Textboxes and click the corresponding button for required operations:

  1. For simply replacing some stringin all the Filtered files, specify the "String to be replaced" and "Replace With" TextBox and click replace.

    simple replacement

  2. For replacing some stringin the entire filtered file, whereas, the stringis found in between specified strings, provide 'StartSearch' and 'EndSearch':

    Replace with condition

  3. For replacing a stringfound anywhere before a specified string, provide 'StartSearch' TextBoxalone, and click on the button.
  4. For Inserting/Replace entire stringwithin some Strings, follow the below steps:
    1. It is essential to provide Stringreplace with TextBox, in the "General String Replacement Group"
    2. Specify 'StartString' and 'EndString' TextBox.
    3. Click on the Buttonbelow this textboxes, so that the contents within the StartString and EndString will be replaced.

      Replace entire String

  5. For Inserting/Replace entire stringif it is available within specified stringblocks.
      Along with the Steps in Operation(4), include the StartBlockand 'EndBlock', then click the button.

    Replace entire String

  6. Inserting some stringin the top or bottom of the file is achieved by using the following code in the sample using the string.insertmethod. The modified stringis written into the file using the WriteContentsToFile Method, which is called inside the ReplaceStringandWriteToFile method.


I'm sure some of the Regex programmers are going to have a better way, some others might use the StringBuilderclass for this to be done for performance reasons. But, I hope someone will find my program useful. As there are possibilities for adding a lot more extended functionality, if you are in need of any such functionality or code changes for some reason, please feel free to contact me, so that I can add those. Thanks for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
I am a software developer from Chennai, India. From Novemeber 2004, I am in to the .NET Framework and I enjoy writing codes in C#. My experience in .NET Technology includes designing and creating custom controls and application for Web and windows platform.

You may also be interested in...

Comments and Discussions

Generaltest Pin
Sivastyle15-May-08 22:45
memberSivastyle15-May-08 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150804.4 | Last Updated 8 Jan 2008
Article Copyright 2007 by Sivastyle
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid