Click here to Skip to main content
12,817,318 members (35,044 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 13 Apr 2009

Adhoc Refactoring with Visual Studio's "Replace in Files"

, 13 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
One of my favorite refactoring tools is Visual Studio's "Replace in Files" dialog


One of my favorite refactoring tools is Visual Studio's "Replace in Files" dialog. It pops up with the touch of the Ctrl+H key combo, sports all kinds of options, and operates against just the current file or set of files.

The most powerful option is the 'Use Regular Expressions' option. There are a few examples in MSDN to help get you started using regular expressions in Visual Studio. From there, you can quickly build up your own regex refactoring expressions library.

Using the Code

Example: Convert a set of automatic properties to the older style that will work in Visual Studio 2005.

This can be handy if you have code in a VS 2008 project that you would like to reuse in VS 2005. If you only have one or two automatic variables, let the compiler catch them and quickly make the changes by hand. But if they are scattered throughout many files, a few pre-canned regex expressions can decimate the amount of manual work required.


  1. Open an empty code file and paste in the sample code below:
    class Claim
        // Works on multi line properties
        string PayerName 
        // Or single line properties
        string PayerId { get;set;}
        string PayerIdQual { get;set;}
  2. Hit Ctrl+H to bring up the Find in Files dialog. Make sure that Quick Replace is selected.
  3. Paste the regular expressions below into the Find What and Replace With text boxes:
    Find What: ^{:Wh*}{:i}:Wh*{:i}:Wh*\{:Wh*get:Wh*;:Wh*set:Wh*;:Wh*\}
    Replace With: \1private \2 m_\3;\n\1\public \2 \3 
    		{ get { return m_\3; } set { m_\3 = value; }}
  4. Click "Replace All".


class Claim
    private string m_PayerName;
    public string PayerName { get { return m_PayerName; } set { m_PayerName = value; }}
    private string m_PayerId;
    public string PayerId { get { return m_PayerId; } set { m_PayerId = value; }}
    private string m_PayerIdQual;
    public string PayerIdQual { get { return m_PayerIdQual; } 
				set { m_PayerIdQual = value; }}

Points of Interest

Generic Properties

You may have asked yourself, "What happens if I have an automatic property with a private or protected setter or the property is a generic collection?" As the scenarios become more complex, decisions and tradeoffs begin to enter the picture.

The regex can be coded to handle multiple cases, but it can quickly become hard to understand and maintain. However going with 'lots of little' regexes can lead to different maintenance issues and it is a hassle to cut and paste all these regexes into the Find and Replace text boxes (however it can still be a huge productivity increase when you have to refactor hundreds of test cases in a hurry).

The next article will look at the strategies and alternative designs available with Visual Studio.

In either case, it is not too difficult to modify regexes once you understand how they work. You will find that with a little practice you can quickly create very powerful expressions.


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


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Moim Hossain13-Apr-09 5:52
memberMoim Hossain13-Apr-09 5:52 
GeneralRe: My vote of 1 Pin
Mike Lang13-Apr-09 7:19
memberMike Lang13-Apr-09 7:19 
GeneralRe: My vote of 1 Pin
conanc13-Apr-09 19:53
memberconanc13-Apr-09 19:53 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 14 Apr 2009
Article Copyright 2009 by conanc
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid