Esc system file names? FROM#

I allow the user to choose any user name that he wants, and it can be anything at all, for example

AC♀¿!$"Man'@ 

Now I need to create a directory for it. What function do I use to avoid text, so I am not a FS problem / exception?

+7
filesystems escaping
source share
6 answers

Use Path.GetInvalidFileNameChars or Path.GetInvalidPathChars to check for character deletion.

http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars.aspx

+3
source share

If you replace invalid characters or delete them, there will always be a chance of collision. If I were you, I would have a separate primary key for the user (possibly a GUID) and use it for the directory name. That way, you can have your usernames, anything, without worrying about invalid directory names.

+10
source share

Depending on whether your characters are ASCII / Unicode, you can use byte / character values ​​as a replacement and use some character to denote these replaced characters (e.g. underscore), giving you something like _001_255_200ValidPart_095_253 . Please note that you must also replace the marking characters ( _095 in the example, 95 is the ASCII code for underlining).

+5
source share

I think it is best to create a dictionary that maps invalid file system characters to a valid replacement string. Then scan the string for invalid characters, replacing the valid strings as you go. This will allow the user to select anything they want and give you consistency to translate it back to the username if you want.

+1
source share

You will not find a way to avoid a username that will give in each instance a valid, non-conflicting name in each instance.

A more robust approach would be to create a directory using some conditional agreement, and then maintain the mapping between them. It also provides support for when your user wants to change the name.

Complete question No. 663520 to find out more about this.

+1
source share

Need to know the exact name of your directory? If not, why not create directories using arbitrary rules and associate them with the owner in the database or something else?

0
source share

All Articles