filename

R

RedLars

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.
 
C

Cowboy \(Gregory A. Beamer\)

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/library/system.io.file_members(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! |
********************************************
 
R

rhaazy

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("\");
 
I

Ignacio Machin ( .NET/ C# MVP )

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.
 
R

RedLars

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.
 
J

Jay Riggs

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
 
G

G.S.

Oops!  You're right, I missed that in the doc.  Sorry!






- 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?
 
I

Ignacio Machin ( .NET/ C# MVP )

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;
}
}
 
J

Jay Riggs

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
 
J

Jay Riggs

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
 
R

RedLars

Just the sort of method I was looking for but unfortunately it
requires atleast .NET 2.0 I'm using .NET 1.1
 
R

RedLars

Just checking for '\' seems uncomplete. I would guess there are other
characters that are invalid in a filename.
 
R

RedLars

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 :)

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
 
J

Jay Riggs

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 :)

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:
^[^ \\/:*?""<>|]+([ ]+[^ \\/:*?""<>|]+)*$
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top