473,506 Members | 11,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does file exist

Hi,

I am looking for function in .Net library that let me know if exist
any file if I specified template.

Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then
I can get True or at least file name . And if does not exist Fasle or
empty string.

Of course I can use VB6 function Dir, but maybe .Net contains
something inside the class library.

Thanks
Nov 20 '05 #1
4 4049
Mike wrote:
Hi,

I am looking for function in .Net library that let me know if exist
any file if I specified template.

Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then
I can get True or at least file name . And if does not exist Fasle or
empty string.

Of course I can use VB6 function Dir, but maybe .Net contains
something inside the class library.


Dim path As String = "c:\test" ' or some other directory
Dim files() As String = System.IO.Directory.GetFiles(path, "*.txt")

--
Sven Groot

http://unforgiven.bloghorn.com
Nov 20 '05 #2
* y1***@yahoo.com (Mike) scripsit:
I am looking for function in .Net library that let me know if exist
any file if I specified template.

Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then
I can get True or at least file name . And if does not exist Fasle or
empty string.


\\\
If System.IO.Directory.GetFiles("C:\foo\*.exe").Lengt h > 0 Then
MsgBox("File(s) exist!")
Else
MsgBox("Files do not exist!")
End If
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Just remember that there is a gotcha when using the

Dim files() As String = System.IO.Directory.GetFiles(path, "*.txt")

functionality where the pattern has a 3 character extension. This will
return information for any file in path having an extension that starts with
..txt, (i.e., .txta, .txtb, .txtaa, .txtab, etc.). For extensions of any
other length this is not an issue. Don't ask me why the Redmond gurus made
it that way (I see it as a bug), but it's in the doc's as a design feature.

"Sven Groot" <sv*******@gmx.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Mike wrote:
Hi,

I am looking for function in .Net library that let me know if exist
any file if I specified template.

Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then
I can get True or at least file name . And if does not exist Fasle or
empty string.

Of course I can use VB6 function Dir, but maybe .Net contains
something inside the class library.


Dim path As String = "c:\test" ' or some other directory
Dim files() As String = System.IO.Directory.GetFiles(path, "*.txt")

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #4
Just remember that there is a gotcha when using the

Dim files() As String = System.IO.Directory.GetFiles(path, "*.txt")

functionality where the pattern has a 3 character extension. This will
return information for any file in path having an extension that starts with
..txt, (i.e., .txta, .txtb, .txtaa, .txtab, etc.). For extensions of any
other length this is not an issue. Don't ask me why the Redmond gurus made
it that way (I see it as a bug), but it's in the doc's as a design feature.

So, for the example to work correclty it needs to be:

Dim _ss() As String = System.IO.Directory.GetFiles("C:\foo", "*.exe")

Dim _f As Boolean = False

For Each _s As String In _ss
If System.IO.Path.GetExtension(_s) = ".exe") Then
_f = True
Exit For
End If
Next

If _f Then
MsgBox("File(s) exist!")
Else
MsgBox("Files do not exist!")
End If
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2h************@uni-berlin.de...
* y1***@yahoo.com (Mike) scripsit:
I am looking for function in .Net library that let me know if exist
any file if I specified template.

Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then
I can get True or at least file name . And if does not exist Fasle or
empty string.


\\\
If System.IO.Directory.GetFiles("C:\foo\*.exe").Lengt h > 0 Then
MsgBox("File(s) exist!")
Else
MsgBox("Files do not exist!")
End If
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #5

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

Similar topics

4
5460
by: Daniel Polansky | last post by:
Is there a way to open file for writing so that the opening fails if the file already exist? This needs to be atomic operation so that it is possible to use the file as a semaphore. As a result,...
11
3582
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
0
2617
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
3
567
by: Olivogt | last post by:
Hello, I was just puting an application on the web server but it did not work as usual... - I do develop on my notebook and move released applications to the Web server - both have Sql Server...
16
4946
by: lawrence k | last post by:
I've a file upload script on my site. I just now used it to upload a small text document (10k). Everything worked fine. Then I tried to upload a 5.3 meg Quicktime video. Didn't work. I've...
3
323
by: Mike | last post by:
Thanks for the reply, I have been trying that, but I keep getting the same results. The result I get is that the file exits, when it really doesn't. All my msgbox display twice and I'm not sure...
1
4177
by: Atia Amin | last post by:
Hi, I am a new member. Hello to every one. I am new in ASP.NET area. I wrote an ASP.NET web application which was running ok with my old laptop. Currently I have given a new laptop. Now I copied...
20
3633
by: Bob Sanderson | last post by:
This is my code: if (file_exists($Fname)) { echo "<td>$Fname exists</td>"; } else { echo "<td>$Fname does not exist</td>"; } $Fname is the full path to the file I'm trying to verify. When I...
2
12583
ADezii
by: ADezii | last post by:
Many times when writing VBA code, we find the need to work with Files in one form or another. Whatever the process may be, we need to be sure that a File is present in a specified location. One...
0
1670
by: KingKong07 | last post by:
Hi! I have a problem with publishing my solution. I use “Use fixed naming and single page assemblies” to publish. So I just can update one and one file, this works fine, but today I had to update...
0
7220
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
7371
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...
1
7023
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...
0
5617
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,...
1
5037
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
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 ...

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.