I have a legacy .NET application that I have ported to .NET 6.0 and am executing cross platform (Windows & Linux).
Much of the legacy code was developed with hard coded path creation, using backslash in strings, like
new StreamWriter(FeuxBarreFolderName + @"\\JazzSchmazz.txt");
that I have converted to something like
new StreamWriter(Path.Combine(FeuxBarreFolderName, "JazzSchmazz.txt"));
I want to prevent any developer from accidentally or purposefully adding \ in a hard coded path again. Other than handling this at code review, is there a way that I can do this automatically, either by code analysis or spectacular failure at testing time? My searches have been unhelpful.
Ideas that I've had
- Modify the ext4 filesystem on my Docker integration tests to prevent filenames with a \. I have no idea if this is possible / reasonable.
- Get creative with permissions on my Docker integration tests to prevent creating files in a root directory (take away w from the parent dir), but allow it on the child dir (add w again to a specific child dir). This feels super brittle.
- Find or write an analyzer to look at strings and complain about any strings that contain a \ character.
Is there a better way here?