473,757 Members | 5,404 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 #1
18 2773
if(File.Exists( filePathFromTex tBox))
{
//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******** *************** ***********@x35 g2000hsb.google groups.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.GetInvalid FilenameChars.

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.netNoS pamMwrote:
if(File.Exists( filePathFromTex tBox))
{
* *//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.feedburne r.com/GregoryBeamer#

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

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

news:9e******** *************** ***********@x35 g2000hsb.google groups.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.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.
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.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.
Sep 24 '08 #10

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
3246
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
1913
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
1785
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
4264
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
9297
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
10069
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
9735
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
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
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
5168
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
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
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
3
2697
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.