RE: Bug in My.Computer.FileSystem.WriteAllText

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi Bo,

The My.Computer.FileSystem.WriteAllText has two variations:

Public Sub WriteAllText( _
ByVal file As String, _
ByVal text As String, _
ByVal append As Boolean _
)
' -or-
Public Sub WriteAllText( _
ByVal file As String, _
ByVal text As String, _
ByVal append As Boolean, _
ByVal encoding As System.Text.Encoding _
)

The first one is using System.Text.Encoding.UTF8 as the default encoding.

The System.Text.Encoding.UTF8 is an instance of
System.Text.Encoding.UTF8Encoding.

The System.Text.Encoding.UTF8Encoding has a constructor which accepts a
boolean parameter named "encoderShouldEmitUTF8Identifier" (see
http://msdn2.microsoft.com/en-us/library/s064f8w2(VS.80).aspx): when it's
true (which is the case for the instance returned by
System.Text.Encoding.UTF8), it means that a Unicode byte order mark (short
for BOM) is provided.

Basically a BOM at the beginning of a file will help the applications that
are reading the file to determine what's the correct encoding of the file.

You may find more information about BOM here:

#Byte-order mark - Wikipedia, the free encyclopedia
http://en.wikipedia.org/wiki/Byte_Order_Mark

#FAQ - UTF-8, UTF-16, UTF-32 & BOM
http://unicode.org/faq/utf_bom.html

#Byte Order Mark
http://msdn2.microsoft.com/en-us/library/ms776429.aspx

In .NET, when a encoding has specified BOM, the IO classes will output
those extra bytes in the beginning of a file when writing the file.

For notepad, it will try to detect the encoding from the BOM:

#The Old New Thing : Some files come up strange in Notepad
http://blogs.msdn.com/oldnewthing/archive/2004/03/24/95235.aspx

Hope this helps.


Sincerely,
Walter Wang (wawang@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

.



Relevant Pages

  • urwid with multi-byte encoded and bidirectional text?
    ... I would like to support whatever encoding the user likes. ... *new* line translation format would have to support characters that are ... N bytes in the string and M columns wide when displayed, ...
    (comp.lang.python)
  • Re: Did Borland doing well in Q4? Listen to the Earning CC
    ... Extend String RTTI to include encoding information. ... The RTL would be extended to support transcoding of strings as required: ... grinding halt in SQL Server 2008. ...
    (borland.public.delphi.non-technical)
  • Re: Web app security
    ... to make the.NET AES encryption to work together with the AES encryption ... BTW, in your code, you first use Unicode Encoding to convert string to ... Microsoft MSDN Online Support Lead ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Surely someone has come across this problem before........
    ... running the configuration wizard again (probably will get you past this ... Event Type: Error ... see Help and Support Center at ... connectionString, SqlFile sqlFileId, String sqlSignaturePath, ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: Surely someone has come across this problem before........
    ... running the configuration wizard again (probably will get you past this ... Event Type: Error ... see Help and Support Center at ... connectionString, SqlFile sqlFileId, String sqlSignaturePath, ...
    (microsoft.public.sharepoint.windowsservices)