Click here to Skip to main content
11,806,026 members (60,954 online)
Click here to Skip to main content

Find and Replace Content of Multi-Resource File using Console Application

, 21 Apr 2014 CPOL 8.1K 83 6
Rate this:
Please Sign up or sign in to vote.
Access Multiple Resource file and find and replace the textual content in resource file

Here, I explain how to find the content in resource file and replace with specified content using console application. Visual Studio does not provide the functionality of find and replace for resources files.

Sample Code

Here, first I load the resource file into XDocument namely xDocResElements in the resource file Element Name Data where I need to make changes.

               XDocument xDocResElements= XDocument.Load(path);

               bool changed = false;
               foreach (XElement xEle in xDocResElements.Root.Elements())
                   if (xEle.Name.LocalName.ToLower() == "data")
                       XElement xValue = (xEle.Element("value") != null) ?
                       xEle.Element("value") : null;

                       if (xValue != null)
                           if (!string.IsNullOrEmpty(xValue.Value))
                               string sValue = xValue.Value;

                               if (!string.IsNullOrEmpty(findText) &&
                     if (sValue.Contains(findText))
                                     changed = true;
                                     xValue.Value = sValue.Replace(findText, replaceText);

               if (changed)
                   Console.WriteLine("Successfully replaced the text, Path :" + path);
           catch (Exception ex)

Replace Code

xValue.Value = sValue.Replace(findText, replaceText); 

Here, I used string function called Replace. We can add more functionality like match only word or any place in the word, etc.

Saving to Location Code


After successful modifications have been made, we save to location.

Now, I will explain the process in a step by step manner.

Step 1

Create a Console application namely (FindAndReplaceContentinResourcefile).

Step 2

Add Resource file to the project.

Step 3

Add some sample data as shown in the below image:

Step 4

Set resource file property namely "Copy to Output Directory as Copy Always".

Step 5

In the example, I used only single resource file but my code is useful for multiple resource files. if we are planning to work with multiple resource files, place all resource file in one folder . In the code, I am reading all resource files from a specified folder.

Sample Code to Read Multiple Resource Files

string resourceFilePath = AppDomain.CurrentDomain.BaseDirectory;

string[] resourceFileNames = System.IO.Directory.GetFiles(resourceFilePath,"*.resx");

foreach (string resFilepath in resourceFileNames)

Here, I am taking BaseDirectrory as the Resource folder.


This code is used to get the files that contain extension of .resx.

This is the time to run the Console application.

Download the source code and build and run the project.

First, it asks for what "Content" needs to be found in the resource file. and second input for Replace the find text in resource file. Here, we specified links.

In the output, it gives success message and path where the resource file is saved with changes.


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


About the Author

Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionStay where it was Pin
Peter Leow20-Apr-14 21:58
professionalPeter Leow20-Apr-14 21:58 
AnswerRe: Stay where it was Pin
munagalasantosh21-Apr-14 0:04
membermunagalasantosh21-Apr-14 0:04 
QuestionThis was a tip... Pin
OriginalGriff20-Apr-14 21:37
protectorOriginalGriff20-Apr-14 21:37 
AnswerRe: This was a tip... Pin
munagalasantosh21-Apr-14 0:05
membermunagalasantosh21-Apr-14 0:05 
GeneralRe: This was a tip... Pin
OriginalGriff21-Apr-14 0:11
protectorOriginalGriff21-Apr-14 0:11 
GeneralRe: This was a tip... Pin
munagalasantosh21-Apr-14 1:07
membermunagalasantosh21-Apr-14 1:07 

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
Web04 | 2.8.151002.1 | Last Updated 21 Apr 2014
Article Copyright 2013 by munagalasantosh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid