473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

filename

Hi,

How can check using .NET 1.1 that a string contains a valid filename
for winxp?

The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.

Any suggestions.
Sep 24 '08 #1
18 2741
if(File.Exists(filePathFromTextBox))
{
//Do work
}
else
{
//Display error
}

Exists has been around since the early days. I believe it was 1.0. It was
definitely 1.1:
http://msdn.microsoft.com/en-us/libr...rs(VS.71).aspx

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"RedLars" <Li***********@gmail.comwrote in message
news:9e**********************************@x35g2000 hsb.googlegroups.com...
Hi,

How can check using .NET 1.1 that a string contains a valid filename
for winxp?

The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.

Any suggestions.
Sep 24 '08 #2
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalidFilenameChars.

http://msdn.microsoft.com/en-us/libr...namechars.aspx

"RedLars" wrote:
Hi,

How can check using .NET 1.1 that a string contains a valid filename
for winxp?

The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.

Any suggestions.
Sep 24 '08 #3
On Sep 24, 9:45*am, RedLars <Liverpool1...@gmail.comwrote:
Hi,

How can check using .NET 1.1 that a string contains a valid filename
for winxp?

The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.

Any suggestions.
Just check your string to make sure there are no occurences of \
..IndexOf("\");
Sep 24 '08 #4
On Sep 24, 9:45 am, RedLars <Liverpool1...@gmail.comwrote:
Hi,

How can check using .NET 1.1 that a string contains a valid filename
for winxp?

The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.

Any suggestions.
I do not of any method in File class that check if a name is valid.
the simplest thing is to check for the \ character. You could use a
regular expression to better check for invalid chars.
Sep 24 '08 #5
OD
the best is certainly using a regular expression.
following sample check long file name validity :
^[^\\\./:\*\?\"<>\|]{1}[^\\/:\*\?\"<>\|]{0,254}$

--
OD___
www.e-naxos.com
Sep 24 '08 #6
Let me re-phrase the question.

By opening notepad, selecting Save as.. and typing in 'g/fgh.txt' the
application wont save the file because the filename is invalid.

How can I using .NET 1.1 check if a given string contains only a valid
filename? It should not include path and filename.

Thank you.

>On 24 Sep, 15:51, "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbwo...@comcast.netNoSpamMwrote:
if(File.Exists(filePathFromTextBox))
{
* *//Do work}

else
{
* * //Display error

}

Exists has been around since the early days. I believe it was 1.0. It was
definitely 1.1:http://msdn.microsoft.com/en-us/libr...members(VS.71)...

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my bloghttp://feeds.feedburner.com/GregoryBeamer#

or just read it:http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! * * * * * * * * * * * * * * * |
********************************************"RedLa rs" <Liverpool1...@gmail.comwrote in message

news:9e**********************************@x35g2000 hsb.googlegroups.com...
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.
Sep 24 '08 #7
On Sep 24, 6:45*am, RedLars <Liverpool1...@gmail.comwrote:
Hi,

How can check using .NET 1.1 that a string contains a valid filename
for winxp?

The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.

Any suggestions.
RedLars,

If you want to check if a given string could be a valid filename only
(even for files that don't exist), you can use a RegEx.

I've been using this one lately:
^[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*$

Which I got here:
http://regexlib.com/REDetails.aspx?regexp_id=2286

A word of warning: using just this regex will not allow you to
identify the several strings Windows will not allow for filenames
(such as nul, com1, etc.). I've been regexs that covered includes
there reserved words can can't find it now.
HTH
-Jay

Sep 24 '08 #8
On Sep 24, 7:48*am, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalidFilenameChars.

http://msdn.microsoft.com/en-us/libr...getinvalidfile...

"RedLars" wrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.- Hide quoted text -

- Show quoted text -

Unfortunately, you can't use this method in 1.1.
Sep 24 '08 #9
Oops! You're right, I missed that in the doc. Sorry!

"Jay Riggs" wrote:
On Sep 24, 7:48 am, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalidFilenameChars.

http://msdn.microsoft.com/en-us/libr...getinvalidfile...

"RedLars" wrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.- Hide quoted text -
- Show quoted text -


Unfortunately, you can't use this method in 1.1.
Sep 24 '08 #10
Exists has been around since the early days. I believe it was 1.0. It was
definitely 1.1:http://msdn.microsoft.com/en-us/libr...members(VS.71)...
I think that he wants to know if a given string is a valid file name,
not to check if a file exist or not
Sep 24 '08 #11
On Sep 24, 1:07*pm, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
Oops! *You're right, I missed that in the doc. *Sorry!

"Jay Riggs" wrote:
On Sep 24, 7:48 am, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalidFilenameChars.
>http://msdn.microsoft.com/en-us/libr...getinvalidfile....
"RedLars" wrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.- Hide quoted text -
- Show quoted text -
Unfortunately, you can't use this method in 1.1.- Hide quoted text -

- Show quoted text -
After checking for invalid characters following the above suggestions,
would it be feasible to try to create a temp file just to see if your
host/target OS will complain?
Sep 24 '08 #12
On Sep 24, 4:27 pm, "G.S." <gstoy...@gmail.comwrote:
On Sep 24, 1:07 pm, Family Tree Mike

<FamilyTreeM...@discussions.microsoft.comwrote:
Oops! You're right, I missed that in the doc. Sorry!
"Jay Riggs" wrote:
On Sep 24, 7:48 am, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalidFilenameChars.
http://msdn.microsoft.com/en-us/libr...getinvalidfile...
"RedLars" wrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.- Hide quoted text -
- Show quoted text -
Unfortunately, you can't use this method in 1.1.- Hide quoted text -
- Show quoted text -

After checking for invalid characters following the above suggestions,
would it be feasible to try to create a temp file just to see if your
host/target OS will complain?
Yes, But that would throw an exception, which is not a very good
approach, you can do something like:

bool isvalid(string str){
try{
string path = Path.GetTempFileName() + str;
File.Create( path).Close();
File.Delete( path);
return true;
}catch{
return false;
}
}

Sep 24 '08 #13
On Sep 24, 1:27*pm, "G.S." <gstoy...@gmail.comwrote:
On Sep 24, 1:07*pm, Family Tree Mike

<FamilyTreeM...@discussions.microsoft.comwrote:
Oops! *You're right, I missed that in the doc. *Sorry!
"Jay Riggs" wrote:
On Sep 24, 7:48 am, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalidFilenameChars.
http://msdn.microsoft.com/en-us/libr...getinvalidfile...
"RedLars" wrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.- Hide quoted text -
- Show quoted text -
Unfortunately, you can't use this method in 1.1.- Hide quoted text -
- Show quoted text -

After checking for invalid characters following the above suggestions,
would it be feasible to try to create a temp file just to see if your
host/target OS will complain?- Hide quoted text -

- Show quoted text -
G.S.

After checking for invalid characters in the filename why not just
attempt to save the file where you want it to go and handle any
exceptions as they occur? This seems the more direct approach unless
I'm missing something. Saving to a temp location successfully only
tells you you can save it to the temp location.

-Jay
Sep 24 '08 #14
On Sep 24, 9:43*am, Jay Riggs <jay.s.ri...@gmail.comwrote:
On Sep 24, 6:45*am, RedLars <Liverpool1...@gmail.comwrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.

RedLars,

If you want to check if a given string could be a valid filename only
(even for files that don't exist), you can use a RegEx.

I've been using this one lately:
^[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*$

Which I got here:http://regexlib.com/REDetails.aspx?regexp_id=2286

A word of warning: using just this regex will not allow you to
identify the several strings Windows will not allow for filenames
(such as nul, com1, etc.). *I've been regexs that covered includes
there reserved words can can't find it now.

HTH
-Jay
Boy, I really mangled the last part of my response above. What I
meant to say was that the regex I provided a link to in my previous
message will check a potential filename for characters that Windows
will not allow, but will not check for filenames that Windows
specifically disallows. Examples include nul, com1, etc.

I found the regex that handles this at the following:
http://regexlib.com/REDetails.aspx?regexp_id=85

I ended up using the modified version found on the first user comment
for the regex. It worked fine for me.

-Jay
Sep 24 '08 #15
Just the sort of method I was looking for but unfortunately it
requires atleast .NET 2.0 I'm using .NET 1.1

On 24 Sep, 16:48, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalidFilenameChars.

http://msdn.microsoft.com/en-us/libr...getinvalidfile...

"RedLars" wrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.
Sep 25 '08 #16
Just checking for '\' seems uncomplete. I would guess there are other
characters that are invalid in a filename.

On 24 Sep, 17:09, rhaazy <rha...@gmail.comwrote:
On Sep 24, 9:45*am, RedLars <Liverpool1...@gmail.comwrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.

Just check your string to make sure there are no occurences of \
.IndexOf("\");
Sep 25 '08 #17
Hi Jay Riggs and OD.

Must admit I'm far from an export on regex's so I was hoping you guys
could very briefly explain your expressions?

They both appear to be split into two parts with different content in
each part.

Appreciate the help :)

On 24 Sep, 18:43, Jay Riggs <jay.s.ri...@gmail.comwrote:
On Sep 24, 6:45*am, RedLars <Liverpool1...@gmail.comwrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename.
Any suggestions.

RedLars,

If you want to check if a given string could be a valid filename only
(even for files that don't exist), you can use a RegEx.

I've been using this one lately:
^[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*$

Which I got here:http://regexlib.com/REDetails.aspx?regexp_id=2286

A word of warning: using just this regex will not allow you to
identify the several strings Windows will not allow for filenames
(such as nul, com1, etc.). *I've been regexs that covered includes
there reserved words can can't find it now.

HTH
-Jay
Sep 25 '08 #18
On Sep 24, 10:14*pm, RedLars <Liverpool1...@gmail.comwrote:
Hi Jay Riggs and OD.

Must admit I'm far from an export on regex's so I was hoping you guys
could very briefly explain your expressions?

They both appear to be split into two parts with different content in
each part.

Appreciate the help :)

On 24 Sep, 18:43, Jay Riggs <jay.s.ri...@gmail.comwrote:
On Sep 24, 6:45*am, RedLars <Liverpool1...@gmail.comwrote:
Hi,
How can check using .NET 1.1 that a string contains a valid filename
for winxp?
The application in question has a textbox where user can enter
filename and only the filename. It should not allowed to enter path
+filename like "c:\tmp\myfile.log" or relative paths "..\tmp
\myfile.log" - no directory info should be allowed, only the filename..
Any suggestions.
RedLars,
If you want to check if a given string could be a valid filename only
(even for files that don't exist), you can use a RegEx.
I've been using this one lately:
^[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*$
Which I got here:http://regexlib.com/REDetails.aspx?regexp_id=2286
A word of warning: using just this regex will not allow you to
identify the several strings Windows will not allow for filenames
(such as nul, com1, etc.). *I've been regexs that covered includes
there reserved words can can't find it now.
HTH
-Jay- Hide quoted text -

- Show quoted text -

RedLars,

Rather than learn the details of these particular regular expressions,
it might be better for you to learn about regular expressions in
general.

I've found various MSDN articles useful and I've found this article
useful:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx
You might also want to check out the multitude of (free) regular
expression designer tools that are available. You can use these to
test and to learn about how regular expressions work. I like this one
(but admit I haven't done a lot of shopping around):
http://www.sellsbrothers.com/tools/#regexd

-Jay
Sep 25 '08 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: S.W. Rasmussen | last post by:
With the risk of being accused of multi-posting I would like to draw the attention to a serious visual basic/windows issue discussed in the microsoft.public.vb.bugs newsgroup. As pointed out below...
11
by: hokiegal99 | last post by:
How would I determine if a filename is greater than a certain number of characters and then truncate it to that number? For example a file named XXXXXXXXX.txt would become XXXXXX fname = files...
4
by: Michael | last post by:
All, I'll asked a question but I was not detailed (clear) enough for people to help so I'll retry. I have a cgi script pass a variable from one page to the next. The 'receiving' page replaces...
10
by: Brian Gruber | last post by:
Hi, I'm looking for a way to rename a whole directory of files in short order. The files in the directory have different lengths, however all of them end with _xxxx the x's represent a randomly...
8
by: Lad | last post by:
Hello, what is a way to get the the extension of a filename from the path? E.g., on my XP windows the path can be C:\Pictures\MyDocs\test.txt and I would like to get the the extension of the...
12
by: IamIan | last post by:
I searched the archives but couldn't find anyone else with this problem. Basically I'm grabbing all ASCII files in a directory and doing geoprocessing on them. I need to calculate a z-factor based...
4
by: B Squared | last post by:
I'm trying to pass a filename (which is a jpeg image) to a php function / file so that it will display. I know that its simple to get PHP to display an image hardcoding in the filename. For...
10
by: Steve | last post by:
I am trying to create a downloader which will bypass the saveas box in the WebBrowser control. I have the file downloading, but I don't know what the filename is that is being passed through the...
0
by: jinnareddy | last post by:
Hi, I'm unable to download a file that is having a 2-byte char in its name (e.g.テ) using force download option. Though, am able to download file names involving ASCII chars. I have tried URL...
12
by: vj | last post by:
I am using Borland C++. I run a program which generates output data files, say 'Voltage.dat', 'Current.dat'. I have a variable in my code (say N), and for various values of N (for each value of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...

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.