473,804 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate File Name (Not File Characters)

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
3 18096
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
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.Co ntains( FirstSectionOfF ilename( filepath ) ) )
{
// Notify user about invalid filename
}

FirstSectionOfF ilename 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****@sturmne t.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
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.Co ntains( FirstSectionOfF ilename( filepath ) ) )
{
// Notify user about invalid filename
}

FirstSectionOfF ilename 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****@sturmne t.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5916
by: deko | last post by:
I use variables in some links on my page. I need to send these variables so I can change the color of the links in the navbar when the user is on that page. If you click around on the site you will see the links in the navbar change: http://www.clearpointsystems.com/ When I try to validate the page at http://validator.w3.org/ I get the following error:
6
2159
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I don´t know much javascript so I wrote a very simple one to validate a form I have on my webpage. could you please have a look at the following script: ------------------------------------------------------------
7
13350
by: Georg J. Stach | last post by:
Hi, as mentioned above I'd like to validate a simple XML-document with a simple DTD. For this, I use Java and Xerces. But, when I have tags of this form: <tag>some characters in here</tag> Xerces always complains with:
0
4593
by: SHC | last post by:
Hi all, I have a VC++ .NET 2003 - Windows XP Pro PC. I created a Win32 console application in my VC++ .NET 2003 and copied validateDOM.cpp, books.xml and books.xsd (see the attached files below) from Microfost MSDN Library for my project "validateXML". When I did "Build" on my project, I got the following fatal error C1010: c:\Documents and Settings\SHC\My Documents\Visual Studio Projects\valoidateDOM\valoidateDOM.cpp(273): fatal error...
6
2754
by: Jonny | last post by:
Hi, I'm trying to validate my xml against a xsd but I can't get it to work. Originally, I wanted to validate an xml string but since I didn't get that to work I tried to validate an xml file instead. Didn't work either. Actuallty, the xml gets loaded but there are no events raised that says my xml is incorrect! I'm using .NET 2.0 and my code is listed below. ALL help is appreciated! Thanks!
24
2112
by: Mike Hofer | last post by:
Please forgive the cross-post to multiple forums. I did it intentionally, but I *think* it was appropriate given the nature of my question. I'm working on an open source code library to help automate and clean up parameter validation code. It's almost ready to go into open beta. But one last little glitch is holding me up, and that would be the name of the factory class that serves as the entry point into the library: Validate.
1
3144
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric numbers. Now i use this script for validate the email address. But it allows the cpital letters otherwise its working correctly. SCRIPT FUNCTION ************************************************
18
2415
vikas251074
by: vikas251074 | last post by:
I am using ASP In my company, all employees have to sent materials out of premises. Any employee who need to sent material out will use this web application. In the first page, an employee enters firm name, vehicle no, driver name, purpose, persons taking out, received by, remarks. In the second page, employee enters no. of materials to be taken out. In the third page, displays no. of rows for entry of material details like material name,...
5
2642
by: ghjk | last post by:
I have a web site with several web pages. They are having different number of variables. eg: first page having 4 variables in the form(Name, Phone, ID, Gender) second page having 10 variables in the form(eg: Name, Age, Time, etc.) I want to validate each and every variable. So how can I do that? I found this JavaScript.But in this case for each and every variable I have to write a function.Eg:for each and every variable i have to rewrite the...
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.