[. Net, NTFS] Filename

  • Thread starter Thread starter Alexander Vasilevsky
  • Start date Start date
A

Alexander Vasilevsky

There is a certain sequence of characters. Must make it a file name. To do
so, should replace all unsafe characters like: \ ", as well as verify the
uniqueness of the name. Does. Net service for such things?

http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
Gift
 
Alexander said:
There is a certain sequence of characters. Must make it a file name. To do
so, should replace all unsafe characters like: \ ", as well as verify the
uniqueness of the name. Does. Net service for such things?
Take a look at Path.GetInvalidPathChars() and Path.GetInvalidFileNameChars()
to filter out invalid characters. File.Exists() can be used to verify that a
file exists, but read the remarks in the documentation carefully
(http://msdn.microsoft.com/library/system.io.file.exists), as it explains
why this doesn't always do what you want.
 
Hello Alexander,

The GetInvalidPathChars method that Jeroen mentioned is a good way to get a
list of invalid characters in paths and filenames.

With this information, you could search and replace them with valid
characters to get a filename. I'm not aware of any ready-made function in
..NET that would do this for you, so you should write your own code to do
that.

Furthermore, you might wish to check for certain special filenames that can
cause confusion. See here for a list for device names that you should avoid:

Naming a File or Directory
http://msdn.microsoft.com/en-us/library/aa365247.aspx

"Do not use the following reserved device names for the name of a file: CON,
PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1,
LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9."

Hope this helps!

--
Regards,

Jani Järvinen
C# MVP
Vantaa, Finland
(e-mail address removed)
 
Back
Top