473,769 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
18 2775
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.mi crosoft.comwrot e:
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.mi crosoft.comwrot e:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalid FilenameChars.
>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.mi crosoft.comwrot e:
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.mi crosoft.comwrot e:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalid FilenameChars.
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.GetTempFil eName() + 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.mi crosoft.comwrot e:
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.mi crosoft.comwrot e:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalid FilenameChars.
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...@gm ail.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.mi crosoft.comwrot e:
You should just need to check if the string contains any invalid filename
characters as returned by Path.GetInvalid FilenameChars.

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.c omwrote:
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...@gm ail.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...@gm ail.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
6800
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 by Norman Diamond the bug may result in loss of data when saving files with the standard common dialog control. Norman Diamond describes the problem as follows: ---------- In VB6 SP6, VB6 SP5, and possibly others, the common dialog box...
11
3249
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 if fname > 6 print fname Thanks!!!
4
1914
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 this variable (in this case %filename%) with whatever was passed. This works fine when the page is called from the cgi script because it has
10
13076
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 generated number from a program that I saved from. So I need to find a way that I can quickly remove the underscore and the last 4 numbers from the filename without changing anything else. I can't set a max length to the file becuase the filename's...
8
8562
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 filename, that is here txt I would like that to work on Linux also
12
1987
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 on the latitude of the ASCII file being worked on, which is in the filename. If I type in the code manually it works and reads the latitude value from the ASCII filename, but when run within ArcGIS it crashes when it gets to int(LatString)....
4
2506
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 example, an href to this: <?php header("content-type:image/jpeg"); $filename = "image_file.jpg"; $im=ImageCreateFromJPEG($filename); ImageJPEG($im);
10
1787
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 stream. Is their a way to determine what the file name is so that I can execute the appropriate functionality of the extensions. The stream is created dynamically so I cannot capture the filename straight from the URL. Thanks STeve
0
2678
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 encoding too, but with no success. Can someone provide details on how to handle the 2-byte char URLs and download the files? Appreciate your suggestions/help in resolving it. Here is my code:
12
4266
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 N), I generate these output data files. After running the program once for a given value of N (say N equal to 1) and when the programme is finished, I manually append the generated filename with the value of N, such as 'Voltage_N1.dat',...
0
9589
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
10216
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
10049
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
9997
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,...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5309
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...
1
3965
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
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.