Valid Windows File Name  
Author Message
cablehead





PostPosted: Visual C# General, Valid Windows File Name Top

Any down and dirty tricks to test a string


Visual C#13  
 
 
Brendan Grant





PostPosted: Visual C# General, Valid Windows File Name Top

Make sure that it is no more than 260 characters long, does not have any trailing spaces or periods.

 
 
RizwanSharp





PostPosted: Visual C# General, Valid Windows File Name Top

First Check for length as Brenden has suggested and then make sure it doesnot contain these characters: " \ / : * " < > |" You can use string.IndexOfAny() to check this and take the decision.

Best Regards,

Rizwan



 
 
ahmedilyas





PostPosted: Visual C# General, Valid Windows File Name Top

I believe if you specify an illegal character when creating a file/folder, it will throw some Exception, either argumentException or some other exception, which I can't quite remember. But in terms of validating input, check the length of the string input, as well as the illegal characters

I think System.IO.Path.InvalidPathChars will contain the array of illegal characters:

http://msdn2.microsoft.com/en-us/library/system.io.path.invalidpathchars.aspx

unfortunately, its obsolete in .NET 2.0 and 3.0

try this:

http://msdn2.microsoft.com/en-us/library/system.io.path.getinvalidpathchars.aspx