Skip to content
| Marketplace
Sign in
Visual Studio Code>Programming Languages>C# Namespace UpdaterNew to Visual Studio Code? Get it now.
C# Namespace Updater

C# Namespace Updater

3raffat

|
6 installs
| (1) | Free
Rename C# namespace declarations based on file location relative to the project's .csproj
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

C# Namespace Renamer

Version License: MIT

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

  1. Open any .cs file.
  2. Right-click anywhere in the editor code area.
  3. Click C#: Rename Namespace.

Method 2: Right-Click in the File Explorer (Single or Multiple Files / Folders)

  1. 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.
  2. Right-click your selection.
  3. Click C#: Rename Namespace.

Method 3: Command Palette

  1. Open any .cs file.
  2. Press Ctrl + Shift + P (or Cmd + Shift + P on macOS).
  3. 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 { }
}

3. Comment & String Safety

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:

  1. Select multiple files or entire directories in the VS Code Explorer:
    📁 Application/       <-- Right click folder
    ├── 📁 Commands/
    │   └── CreateUser.cs
    └── 📁 Queries/
        └── GetUser.cs
    
  2. Right-click $\rightarrow$ C#: Rename Namespace.
  3. All .cs files inside are updated automatically (ignoring bin/, obj/, .git/, .vs/, etc.).
  4. 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:

  1. Find Project: Walks upward from the .cs file to find the nearest .csproj.
  2. Root Namespace: Reads <RootNamespace> from the .csproj. If omitted, uses the project name (e.g. MyProject.csproj $\rightarrow$ MyProject).
  3. Relative Directory: Takes the directory of the file relative to the .csproj folder.
  4. 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.

  1. Press Ctrl + K, Ctrl + S (or Cmd + K, Cmd + S on macOS) to open Keyboard Shortcuts.
  2. Click the Open Keyboard Shortcuts (JSON) icon in the top right.
  3. 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.

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
  • Your Privacy Choices
  • Consumer Health Privacy
© 2026 Microsoft