.NET Tips and Tricks

Blog archive

Exploiting the Find and Replace Dialog

One of the best things about .NET is that virtually everything you do in Visual Studio generates some text in a file. This means that, when you need to make a change to several places in your code, you can often make that change with a global Find and Replace. However, the typical developer's scenario is to (a) do a global find-and-replace and then (b) rebuild the application to find out what got broken -- because, sadly, a global find-and-replace often changes too much.

The Find In Files option (available from the Find and Replace choice on the Edit menu or from the dropdown lists at the top of the Find dialog) can be a big help here because it lets you specify what kind of files to search. Find in Files may not prevent you from changing things you didn't intend to, but it can limit the damage. For instance, if you know that you want to change the name of a variable in your code, you can limit your search to files matching *.aspx.cs (or *.aspx.vb). That will, at least, prevent you from changing some text tucked away in a file generated by a visual designer. The Files option even comes with a dropdown list of some typical file groups.

As long as you're using the Find and Replace dialog, this dialog is also the best reason to learn regular expressions. For instance, I recently got saddled with this text appearing about 200 times in, roughly, 20 aspx files spread across about two dozen projects:

<div id="somevalue" class="samevalue">

Since my CSS selectors were tied to the element's id attribute, the class value was completely redundant. Having time on my hands, I wanted to clean up the code and convert all of the div tags to this:

<div id="somevalue">

The problem was that while the id and class in any particular tag had the same value, each div tag had its own special value in the id and class attributes. Normally this would have been too much work to bother with. but using the Find and Replace dialog with a regular expression made it trivially easy. Here are the three settings I used:

Find in files: id="{.*}" class="\1"
Replace in files: id="\1"
Files: *.aspx

A button click, a few seconds wait, and all of the elements in the project were cleaner.

You might think that I had to repeat this process in every project. But one of the best parts of developing in ASP.NET is that you can open any folder as a Web site (just use File | Open Website). In a Web site all the files in the folder (and its subfolders) are part of the project. So by opening the folder that contained all of the projects that I wanted to change I was able to run the change across all 20 projects.

I'd love to take credit for this tip, but all credit goes to my very clever friend, Nigel Armstrong. Got a tip you'd like to share? Email me at phvogel@1105media.com.

Posted by Peter Vogel on 07/12/2011 at 1:16 PM


comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.