C# Namespace Renamer

A lightweight, robust Visual Studio Code extension that updates C# namespace declarations to match their file folder structure relative to the project's .csproj.
Quick Start (3 Ways to Use)
Method 1: Right-Click in the Code Editor
- Open any
.cs file.
- Right-click anywhere in the editor code area.
- Click C#: Rename Namespace.
Method 2: Right-Click in the File Explorer (Single or Multiple Files / Folders)
- In the VS Code File Explorer sidebar, select:
- A single
.cs file, OR
- Multiple
.cs files (hold Ctrl or Shift while clicking), OR
- Entire folders containing
.cs files.
- Right-click your selection.
- Click C#: Rename Namespace.
Method 3: Command Palette
- Open any
.cs file.
- Press
Ctrl + Shift + P (or Cmd + Shift + P on macOS).
- Type C#: Rename Namespace and press
Enter.
Step-by-Step Walkthrough & Example
Scenario: You moved a file to a new folder
Suppose your project is structured like this:
MyProject/
├── MyProject.csproj
└── Domain/
└── Entities/
└── User.cs
Initially, User.cs has:
namespace MyProject.Domain.Entities;
public class User
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
Now you move User.cs into Application/Users/:
MyProject/
├── MyProject.csproj
└── Application/
└── Users/
└── User.cs
Running the extension:
Right-click User.cs $\rightarrow$ C#: Rename Namespace.
Result:
The namespace declaration is cleanly updated, preserving the rest of your file and code formatting:
namespace MyProject.Application.Users;
public class User
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
A notification confirms the change:
Namespace updated:
MyProject.Domain.Entities
→
MyProject.Application.Users
Supported C# Styles
1. File-Scoped Namespaces (C# 10+)
// Before
namespace MyProject.OldFolder;
// After
namespace MyProject.NewFolder;
2. Block-Scoped Namespaces
// Before
namespace MyProject.OldFolder
{
public class Order { }
}
// After
namespace MyProject.NewFolder
{
public class Order { }
}
The extension uses a token-aware scanner that never accidentally modifies:
- Commented-out namespaces:
// namespace Old.Name; or /* namespace Old.Name; */
- String literals:
string text = "namespace Old.Name;";
- Verbatim strings:
@"namespace Old.Name;"
- Interpolated strings:
$"namespace {name};"
- Raw string literals:
""" namespace Old.Name; """
Multi-File & Folder Batch Renaming
You can update dozens or hundreds of files in seconds:
- Select multiple files or entire directories in the VS Code Explorer:
📁 Application/ <-- Right click folder
├── 📁 Commands/
│ └── CreateUser.cs
└── 📁 Queries/
└── GetUser.cs
- Right-click $\rightarrow$ C#: Rename Namespace.
- All
.cs files inside are updated automatically (ignoring bin/, obj/, .git/, .vs/, etc.).
- A summary notification is displayed:
Processed 8 C# files: 6 updated, 2 already correct.
How the Namespace is Calculated
The extension computes the target namespace using the following rules:
- Find Project: Walks upward from the
.cs file to find the nearest .csproj.
- Root Namespace: Reads
<RootNamespace> from the .csproj. If omitted, uses the project name (e.g. MyProject.csproj $\rightarrow$ MyProject).
- Relative Directory: Takes the directory of the file relative to the
.csproj folder.
- Combines:
[RootNamespace].[Folder].[SubFolder]
- If the file is in the project root:
[RootNamespace]
- If the file is in
Services/Auth: [RootNamespace].Services.Auth
Settings
Open VS Code Settings (Ctrl + , or Cmd + ,) and search for C# Namespace Renamer:
| Setting |
Default |
Description |
csharpNamespaceRenamer.insertNamespaceIfMissing |
false |
When enabled, if a C# file has no namespace declaration at all, the extension inserts the calculated file-scoped namespace at the top of the file (after any using directives or headers). |
Optional: Add a Keyboard Shortcut
You can assign a custom keyboard shortcut to rename namespaces instantly.
- Press
Ctrl + K, Ctrl + S (or Cmd + K, Cmd + S on macOS) to open Keyboard Shortcuts.
- Click the Open Keyboard Shortcuts (JSON) icon in the top right.
- Add:
{
"key": "ctrl+alt+r",
"command": "csharpNamespaceRenamer.renameNamespace",
"when": "editorLangId == csharp"
}
Now you can press Ctrl + Alt + R inside any .cs file to rename its namespace immediately!
Troubleshooting & FAQ
- "This command can only be used with C# files."
Ensure the active file or selection is a .cs file.
- "Could not find a C# project for this file."
The extension searches upward for a .csproj. Ensure the file is part of a C# project containing a .csproj file in its directory tree.
- "Namespace is already correct."
The namespace declaration in the file already matches the folder structure. No changes were necessary.
- "No namespace declaration found."
The file does not have a namespace declaration. If you want the extension to insert one automatically, enable the csharpNamespaceRenamer.insertNamespaceIfMissing setting.
License
MIT License. See LICENSE for details.