20

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?

Bioinformagician
  • 643
  • 6
  • 10

2 Answers2

34

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.

ogggre
  • 2,204
  • 1
  • 23
  • 19
Bioinformagician
  • 643
  • 6
  • 10
  • 7
    What an absolute horrific feature. Thank you for finding this, was searching all over the place in options. – Kristin Jun 17 '22 at 14:02
  • 5
    I choose **Don't ask again** and **No** and suddenly all namespaces are getting changed... Thanks for this! It doesn't show up in the search... – Mikael Dúi Bolinder Nov 14 '22 at 22:49
  • I experienced the same as Mikael - despite clicking "Don't ask again" then clicking No, subsequent file moves triggered a rename, as if I had actually answered "Yes". After searching VS settings for the text "namespace", I couldn't find anything remotely matching this feature, so this answer explains why! – Andrew Stephens Apr 26 '23 at 08:34
  • @Kristin horrific? Under what circumstances would you NOT want the namespaces to change when you move a file to another folder? I would think they would be very, very seldom. We've been waiting for this feature for years so we don't have to manually fix up all the namespaces when you move a file. Given that the default is to prompt you when you first install VS and you can choose "don't prompt me again" with whatever your choice is, I can't see how this could be even easier to accommodate the few people who wouldn't want this behavior. – rory.ap May 09 '23 at 12:32
  • 1
    @rory.ap Horrific was the wrong word; and was probably fueled by the fact that this feature's necessity doesn't apply to me. That you/your team have been waiting for this for years, doesn't change the fact that the feature changed default behavior. The prompt never came up for me/my team, and the feature was enabled by default upon updating. I wouldn't mind sharing my thoughts as to why I don't need the feature, but the limited length of comments makes it almost impossible, unfortunately. The jest of it: type serialization (yep, disgusting) and namespace != folder structure (yes, contentious). – Kristin May 09 '23 at 14:20
5

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.

ogggre
  • 2,204
  • 1
  • 23
  • 19