IceAzul Posted March 28, 2007 Posted March 28, 2007 Is there a System.IO method that can analyze a filename for validity? Quote
Administrators PlausiblyDamp Posted March 29, 2007 Administrators Posted March 29, 2007 System.IO.Path.InvalidPathChars is an array of invalid characters (good choice of name really) - you could always do an .IndexOf on these characters against a possible filename to check for the existence of invalid characters. Other than that checking the length against the maximum allowed length (255 characters IIRC) is probably all you would need to do. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted March 29, 2007 Leaders Posted March 29, 2007 Depending on what you are doing, you might want to use System.IO.Path to extract the containing directory and then System.IO.Directory.Exists (I think such a function exists) to verify that the path exists. Quote [sIGPIC]e[/sIGPIC]
amir100 Posted March 29, 2007 Posted March 29, 2007 If you're creating a new file than you should check that the Directory exist using System.IO.Directory.Exists like marble_eater suggested. Then you have to check the filename for invalid characters like PlausiblyDamp said. But if the file already exist then I'd suggest directly using System.IO.File.Exists to check the file. Hope it helps. Quote Amir Syafrudin
IceAzul Posted March 29, 2007 Author Posted March 29, 2007 The file doesn't exist yet, I just want to check if the name is valid. I was just checking if there was some method in the System.IO namespace, seems kind of weird that there is none. One more thing, I think the char array I want to use is InvalidFileNameChars (I think the InvalidPathChars is for when you are parsing an entire path, is this correct?) Quote
Nate Bross Posted March 29, 2007 Posted March 29, 2007 Possibly this function may be useful. (Not at compiler, but I think it will work.) public Boolean IsFileNameValid(String sFileName) { foreach(char c in System.IO.Path.InvalidPathChars) { if(sFileName.IndexOf(c) > 0) return false; } return true; } Public Function IsFileNameValid(ByVal sFileName As String) As Boolean For Each c As Char In System.IO.Path.InvalidPathChars If sFileName.IndexOf(c) > 0 Then Return False End If Next Return True End Function Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
IceAzul Posted March 29, 2007 Author Posted March 29, 2007 I think the code should be (!= -1) because IndexOf() returns -1 if its not in the array (0 is a valid position). Quote
amir100 Posted March 30, 2007 Posted March 30, 2007 I think the code should be (!= -1) because IndexOf() returns -1 if its not in the array (0 is a valid position). You're correct. But I'd prefer sticking with (... > -1). :D Quote Amir Syafrudin
amir100 Posted March 30, 2007 Posted March 30, 2007 The property System.IO.Path.InvalidPathChars doesn't include characters such as '/' or '\'. Adding to that, the property itself si considered obsolete. .Net suggest using the method System.IO.Path.GetInvalidPathChars(). Anyway to check valid file name we should use System.IO.Path.GetInvalidFileNameChars(). The method proposed by Nate is good enough. Still we need to replace the InvalidPathChars with GetInvalidFileNameChars. I believe this wraps up the problem. Quote Amir Syafrudin
Administrators PlausiblyDamp Posted March 30, 2007 Administrators Posted March 30, 2007 I suggested InvalidPathChars so as not to confuse anyone on .Net 1 - the warning will be enough to point any .Net 2 developers to the correct method. Rather than calling .IndexOf in a loop you could just call .IndexOfAny and pass in the array of characters returned be InvalidPathCharacters (or GetInvalidFileNameChars). Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
amir100 Posted March 30, 2007 Posted March 30, 2007 I see. I'm currently using .Net 2.0. I didn't consider .Net 1. Sorry for that. I also agree on using IndexOfAny. My bad. :D Quote Amir Syafrudin
Wraith Posted March 30, 2007 Posted March 30, 2007 System.IO methods do not allow stream syntax and do not check for reserved names. If you want to do these things you have to do it yourself. Quote
joe_pool_is Posted April 5, 2007 Posted April 5, 2007 Lazy way: Try to create the file. If it fails, report that the file could not be created. If you are using a try ... catch block, you can even display why. I can remember creating file names with weird stuff like "one©file.txt" back in the DOS days. Most PC users did not know how to create the "©" character from the keyboard, so the file was somewhat safe. And Win95 would not even display the file in Explorer. But I digress. Quote Avoid Sears Home Improvement
amir100 Posted April 17, 2007 Posted April 17, 2007 Your lazy way needs two IO operation. Creating the test file and the deleting the text file. I'd never recommend this. Not even when I'm being lazy. :D Besides we cannot assume a file has an invalid name if it fails to be created. An example would be exceptions resulting from security issues. CMIIW. Quote Amir Syafrudin
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.