By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,740 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,740 IT Pros & Developers. It's quick & easy.

Validate File Name (Not File Characters)

P: n/a
Hi all,

I know that we can check for invalid file name characters as shown at
http://www.codeproject.com/useritems...usefulpath.asp.

But how can we check a string for invalid file name such as CLOCK$,
AUX, CON, NUL, PRN, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8,
COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9?

There is a C++ implementation of it at
http://www.codeproject.com/file/isvalidfilename.asp. But anyone knows
how to implement it in C#?

Thanks in advance.

Jan 11 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hello sheeeng,
>There is a C++ implementation of it at
http://www.codeproject.com/file/isvalidfilename.asp. But anyone knows
how to implement it in C#?
Not sure I understand your question - translating the C++ code to C# seems
like a logical idea. Otherwise, the rules listed in the article don't look
very difficult to implement, so you might do better just reimplementing
the whole thing following those rules.
Oliver Sturm
--
http://www.sturmnet.org/blog
Jan 11 '07 #2

P: n/a
I don't know how you may retrieve reserved device names, but assuming the
list in the article is complete, simply create a list of all the device
names (for instance a List<string>) and check the filename against this
list

[Pseudocode]
if( ReservedList.Contains( FirstSectionOfFilename( filepath ) ) )
{
// Notify user about invalid filename
}

FirstSectionOfFilename retrieves the first part of the filename ending at
the first dot, using SubString and IndexOf(".")

On Thu, 11 Jan 2007 13:01:24 +0100, Oliver Sturm <ol****@sturmnet.org>
wrote:
Hello sheeeng,
>There is a C++ implementation of it at
http://www.codeproject.com/file/isvalidfilename.asp. But anyone knows
how to implement it in C#?

Not sure I understand your question - translating the C++ code to C#
seems like a logical idea. Otherwise, the rules listed in the article
don't look very difficult to implement, so you might do better just
reimplementing the whole thing following those rules.
Oliver Sturm


--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 11 '07 #3

P: n/a
Thanks! I will try implement your Pseudocode now.

Morten Wennevik [C# MVP] wrote:
I don't know how you may retrieve reserved device names, but assuming the
list in the article is complete, simply create a list of all the device
names (for instance a List<string>) and check the filename against this
list

[Pseudocode]
if( ReservedList.Contains( FirstSectionOfFilename( filepath ) ) )
{
// Notify user about invalid filename
}

FirstSectionOfFilename retrieves the first part of the filename ending at
the first dot, using SubString and IndexOf(".")

On Thu, 11 Jan 2007 13:01:24 +0100, Oliver Sturm <ol****@sturmnet.org>
wrote:
Hello sheeeng,
There is a C++ implementation of it at
http://www.codeproject.com/file/isvalidfilename.asp. But anyone knows
how to implement it in C#?
Not sure I understand your question - translating the C++ code to C#
seems like a logical idea. Otherwise, the rules listed in the article
don't look very difficult to implement, so you might do better just
reimplementing the whole thing following those rules.
Oliver Sturm

--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.