Friday, September 19, 2025

Visual Studio: Finding Using Statements Added to Source Code by Visual Studio

In a previous post (Visual Studio: Disabling Add Using Statements on Paste), I showed that Visual Studio has a feature that adds using statements to C# files even when a project takes advantage of project-level global usings. And yes, this feature does turn itself back on (see below -- it's back):



Periodically, in a large project, I like searching for these superfluous using statements. The way to do this is to use a regular expression. From Visual Studio, select Find in Files, which is under Edit → Find and Replace → Find in Files (CTRL+SHIFT+F). In the search textbox, paste the following regular expression:

(?m)^using\s+

Make sure you select the "Use regular expressions" checkbox in the "Find and Replace" dialog (see below):


File types can be *.* or *.cs. Click on "Find All" and purge away.

Below is ChatGPT's breakdown of the regular expression,  (?m)^using\s+ :


No comments :

Post a Comment