PC Review


Reply
Thread Tools Rate Thread

Checking If File Exists...Best Method

 
 
Peter
Guest
Posts: n/a
 
      11th Aug 2004
Currently I'm using the method below, is there someting more efficient?:

Imports System.IO
Public Class CountLine
Public Shared Function CountLines(ByVal FileName As String) As Integer
Dim fs As IO.FileStream
Dim sr As IO.StreamReader
Dim Result As Integer
Try
fs = New IO.FileStream( _
FileName, IO.FileMode.Open, _
IO.FileAccess.Read, IO.FileShare.Read _
)

sr = New IO.StreamReader(fs)

Do
If sr.ReadLine Is Nothing Then Exit Do

Result += 1
Loop

Return (Result)
Catch
Result = 0
End Try

End Function



End Class
 
Reply With Quote
 
 
 
 
Simon Jefferies
Guest
Posts: n/a
 
      11th Aug 2004
Hello,

Try:-

If System.IO.File.Exists("filename") Then

...

End If

Regards
Simon


"Peter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Currently I'm using the method below, is there someting more efficient?:
>
> Imports System.IO
> Public Class CountLine
> Public Shared Function CountLines(ByVal FileName As String) As Integer
> Dim fs As IO.FileStream
> Dim sr As IO.StreamReader
> Dim Result As Integer
> Try
> fs = New IO.FileStream( _
> FileName, IO.FileMode.Open, _
> IO.FileAccess.Read, IO.FileShare.Read _
> )
>
> sr = New IO.StreamReader(fs)
>
> Do
> If sr.ReadLine Is Nothing Then Exit Do
>
> Result += 1
> Loop
>
> Return (Result)
> Catch
> Result = 0
> End Try
>
> End Function
>
>
>
> End Class



 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      12th Aug 2004
And for countling lines is there something better?
 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th Aug 2004
* (E-Mail Removed) (Peter) scripsit:
> And for countling lines is there something better?


Looping through the lines until the end of the file is reached and
counting the lines read from the files is the best approach.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      13th Aug 2004
On 13 Aug 2004 01:23:34 +0200, Herfried K. Wagner [MVP] wrote:

> * (E-Mail Removed) (Peter) scripsit:
>> And for countling lines is there something better?

>
> Looping through the lines until the end of the file is reached and
> counting the lines read from the files is the best approach.


That depends on the structure of the file. If all the lines are the same
length, then you can take the filesize and divide by the line length.

But if the lines in the file are variable length, the what Herfried
suggested is probably the best way.


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking to see if a file exists. JHB Microsoft Excel Misc 3 5th Aug 2009 03:11 PM
Simple Method of Checking if a Record Exists robboll Microsoft Access Form Coding 1 3rd Jun 2006 07:56 AM
Routine for checking if file exists Centurion Microsoft Excel Programming 6 26th Jan 2005 11:02 PM
Checking if a file exists =?Utf-8?B?YXJp?= Microsoft Access Getting Started 2 5th May 2004 07:14 PM
Checking if file exists in VBA NADavies Microsoft Excel Programming 3 23rd Oct 2003 02:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:50 AM.