1

i stumbled upon this bit here in a project from a colleague:

foreach (var invalidChar in Path.GetInvalidFileNameChars())
    fileName = fileName.Replace(invalidChar, '\0');

the gerenal idea is obvious enough but i wonder why he chose to replace the invalid chars with the literal for the null char instead of a 'regular' char or just an empty string.

i guess there's a good reason for this choice(the guy who wrote this is a senior in our team), i'd just like to know what this reason is.

garglblarg
  • 576
  • 1
  • 8
  • 16
  • 2
    I'd say that this is even plain wrong... My first guess is that this is from old C code where strings a null-terminated. MSDN states that characters from 1 to 31 + some others are invalid, which doesn't include \0, but I'm pretty sure that \0 will cause problems. – Thorsten Dittmar Jul 20 '16 at 08:12
  • 1
    Ask your senior developer if he is still on your team – Jehof Jul 20 '16 at 08:14
  • @ThorstenDittmar mkay... what would you suggest instead? – garglblarg Jul 20 '16 at 08:14
  • @Jehof: he's on vacation for the next few weeks but the client is complaining about something, that's why i have to read the project in the first place. so i'd love to ask him but that's not an option :/ – garglblarg Jul 20 '16 at 08:16
  • 3
    @garglblarg I'd either replace by empty string or something like an underscore so you can *actually see* that something was replaced. As I said in my edited comment I'm pretty sure that having a \0 in a file name will either not work in the first place or cause problems with software where \0 marks the end of a string. – Thorsten Dittmar Jul 20 '16 at 08:16
  • Oh, and for *i guess there's a good reason for this choice(the guy who wrote this is a senior in our team)*: Do you read http://thedailywtf.com? – Thorsten Dittmar Jul 20 '16 at 08:21
  • @ThorstenDittmar regarding thedailywtf.com: nope, at least not regularly. – garglblarg Jul 20 '16 at 08:28
  • @garglblarg You should - it will rectify some of the false assumptions about seniors :-D – Thorsten Dittmar Jul 20 '16 at 08:29
  • @ThorstenDittmar will do... since my perception on this topic might be a bit warped by thecodinglove, which i already observe daily x) – garglblarg Jul 20 '16 at 08:34

3 Answers3

5

After commenting the question I was looking for proof that \0 is actually not allowed for file names. I found it:

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
* The following reserved characters: < (less than), > (greater than), : (colon), " (double quote), / (forward slash), \ (backslash), | (vertical bar or pipe), ? (question mark), * (asterisk)
* Integer value zero, sometimes referred to as the ASCII NUL character.
* Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
3

It depends on the OperationSystem where your code runs. But on Windows the char \0 (0 as int) is on the list of invalid chars for a fileName.

LinqPad (run on Windows 10):

Path.GetInvalidFileNameChars().Contains('\0').Dump(); //true

I think this code was ported from another language to .net.

It would be better to throw an exception (if an user specified the name) if the filename contains invalid chars instead of replacing them with anything.

If you need to replace them you should select a char, like _, to make it clear that there was possibly something replaced.

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • this is supposed to run in windows servers, so if '\0' is an invalid char itself that would be quite the pointless exercise. i'll make sure to consult the msdn on this. – garglblarg Jul 20 '16 at 08:31
0

As per some wise people there is no thing as empty char. Also should avoid confusion about space (" ") and empty string ("").

Community
  • 1
  • 1
Janis S.
  • 2,526
  • 22
  • 32