Have determined that the file was in utf8 format...readalltext return "" if
it is in Unicode format..weird!

 

From: [email protected] [mailto:[email protected]]
On Behalf Of Joseph Clark
Sent: Tuesday, 16 November 2010 8:52 AM
To: ozDotNet
Subject: Re: OPen File not working...

 

Definitely sounds like it could be an encoding problem.  You could try
replacing this function of yours with System.IO.File.ReadAllText
<http://msdn.microsoft.com/en-us/library/system.io.file.readalltext.aspx> ,
which should attempt to automatically detect the file encoding for you.

Alternatively, open the file in a decent text editor (like Notepad++), which
will be able to tell you the encoding of the file.

Also, have you checked if an exception is being thrown? I notice you are
catching it and printing it to trace output. Any leads there?

Joe.





On Mon, Nov 15, 2010 at 10:32 PM, Jano Petras <[email protected]> wrote:

Hi Anthony,

It seems related to encoding mark (see
http://en.wikipedia.org/wiki/Byte_order_mark).

Just throwing few ideas into the air here as haven't really tried this
myself - but maybe you could try using StreamReader constructor overload
that accepts encoding so you could pass Encoding.Unicode to see if it solves
the issue.  If not, reading the file as stream of bytes and then use the
Encoding converter to convert it to string or strip off the byte order mark
at the beginning and convert then?

There is a similar topic discussed here
http://stackoverflow.com/questions/1835430/byte-order-mark-screws-up-file-re
ading-in-java but uses Java to explain the issue and resolution.


Cheers,
jano






On 15 November 2010 10:10, Anthony <[email protected]> wrote:

I have been using this function for years with no problem..now when i try to
open a file(contains XML)..it returns an empty string even though it has
text.  I have noticed that the XML  fie has some null charcaters at the
begining..any suggestion?

Public Shared Function GetFileContents(ByVal FullPath As String) As String

       Dim stream As FileStream = Nothing

       Dim strContents As String = String.Empty

        Try

            stream = File.Open(FullPath, FileMode.Open, FileAccess.Read)

            Dim Tin As System.IO.StreamReader = New
System.IO.StreamReader(stream)

            strContents = Tin.ReadToEnd()

            Tin.Close()

            stream.Close()

            Return strContents

        Catch ex As Exception

            Trace.WriteLine(ex.Message)

            Return ""

        Finally

            If Not stream Is Nothing Then

                stream.Close()

            End If

        End Try

    End Function

 <http://www.intellixperience.com/signup.aspx> Is your website being
IntelliXperienced?  | www.yougoingmyway.com ?
regards
Anthony (*12QWERNB*)

Is your website being IntelliXperienced?

 

 

 

Reply via email to