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

Viewed 679

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?

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.

And if you ever 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 This.Namespace.Stays.Intact
{
  ...
}

From the first sight 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 namespace-updated.

Related