Finding all Namespaces in a C# Project or Solution

To get an overview on which namespaces are declared in a (known or unknown) C# project or solution, you can simple run your favorite grep tool on your source directories:

grep -r "^namespace " *  --include=*.cs --exclude-dir=*Test* 
    --exclude=*designer.cs > files-namespaces.txt

Here, the -r switch causes recursive search through all subdirectories, –include names the files to search, –exclude-dir and –exclude the directories and files to exclude from the search. The result is written to a file.

This will produce a list of all namespace declarations.

Using the uniq command of the command line utilities we can find all unique namespaces:

grep -r "^namespace " *  --include=*.cs --exclude-dir=*Test* 
    --exclude=*designer.cs -h | sort 
    | uniq > namespaces.txt

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: