By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

Does file exist

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.