Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Reading a text file into a string?
Message
 
 
À
17/03/2007 15:13:36
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01205023
Message ID:
01205047
Vues:
33
>>I have used the following method of reading a text file using StreamRead object:
>>
>>
>>FileInfo oFileInfo = new FileInfo(path);
>>StreamReader oReader = oFileInfo.OpenText();
>>string FileString = oReader.ReadToEnd();
>>oReader.Close();
>>
>>
>>My question is if there a more efficient method? (like FILETOSTR() in VFP).
>
>I have pretty much the same thing:
>
>
>        ' FileToStr() VFP equivalent
>        ' expC1 File name
>        Public Function FileToStr(ByVal tcFileName As String) As String
>            Dim lcString As String = ""
>            Dim lnCounter As Integer = 0
>            Dim lnDelay As Integer = 250
>            Dim lnRetry As Integer = 8
>            Dim loFile As IO.StreamReader
>            For lnCounter = 1 To lnRetry
>                Try
>                    loFile = New IO.StreamReader(tcFileName, True)
>                    lcString = loFile.ReadToEnd()
>                    loFile.Close()
>                    Exit For
>                Catch loError As Exception
>
>                    ' If we have not reached the maximum tries of 8, we retry
>                    If lnCounter < 8 Then
>
>                        ' Wait for a timeout before retrying
>                        System.Threading.Thread.Sleep(lnDelay)
>
>                        Continue For
>                    End If
>
>                    ' Get the proper definition as per the current scope
>                    If oProcess Is Nothing Then
>                        oApp.ErrorSetup(loError)
>                    Else
>                        oProcess.ErrorSetup(loError)
>                    End If
>
>                End Try
>            Next
>            Return lcString
>        End Function
>
If you have the same thing, this must be the most efficient way. Thank you very much. I will add the error code based on your example.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform