Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop Visual Studio 2022 from automatically changing namespaces and using statements when moving a file?

VisualStudio 2022 v17.2.3 has suddenly started changing the namespaces of my C# files when I drag them to a different directory. In the process it also deletes/adds/modifies using statements (often deleteriously) and updates every file referencing the moved file. It's changing the namespace to the ProjectName.FolderPath.

Searching Tools > Options has turned up nothing. I've disabled all extensions in case one of them was responsible, but it's had no effect. How can this behaviour be stopped?

like image 315
Bioinformagician Avatar asked Sep 13 '25 01:09

Bioinformagician


2 Answers

To disable this globally, go to

Tools > Options > Projects and Solutions > General

and untick the box near the bottom next to "Enable namespace update when moving files". There is a bug in VS2022 that prevents this option being displayed when searched.

like image 166
Bioinformagician Avatar answered Sep 15 '25 03:09

Bioinformagician


If you need to disable the namespace update on a per file basis (as opposed to the global setting) then you can add the following directive to the file:

#pragma warning disable IDE0130 // Namespace does not match folder structure
    
namespace This.Namespace.Stays.Intact
{
    // ...
}

From the first glance it disables the warning, but it also disables the namespace update for that file. Which is super handy when you want to exclude that single file from being modified on, say, namespace sync.

like image 44
ogggre Avatar answered Sep 15 '25 04:09

ogggre