Posted 13 years ago by Kasper
Version: 3.1.0213
Avatar
Hi,

One of my users are currently complaining that a BOM is added in the top of the file, when saving with the UTF-8 encoding. Apparently this is a problem when writing PHP code and saving as UTF-8. How can I prevent the SyntaxEditor from writing the BOM? :)

Comments (4)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
We don't even know what a BOM is. :)

Our save file code is as straightforward as it comes:
public void SaveFile(string path, LineTerminator lineTerminator) {
    StreamWriter writer = File.CreateText(path); 
    writer.Write(this.GetText(lineTerminator)); 
    writer.Flush(); 
    writer.Close();
}
The other SaveFile overloads will use a StreamWriter with the Encoding you specify. Maybe try one of those? Sorry I can't be of more assistance but it's probably something in the .NET framework that is doing it.


Actipro Software Support

Posted 13 years ago by Kasper
Avatar
I already use the Save overload which takes an encoding as parameter, to save the file with the user specified encoding :/

Here is a bit more information from the MSDN: http://msdn2.microsoft.com/en-US/library/system.io.streamwriter.aspx

Perhaps you can tell me how to solve this problem with this new knowledge? :P

[Modified at 10/30/2006 10:13 AM]
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
That code is simply this:
public void SaveFile(string path, Encoding encoding, LineTerminator lineTerminator) {
    FileStream stream = File.Create(path);
    StreamWriter writer = new StreamWriter(stream, encoding);
    writer.Write(this.GetText(lineTerminator)); 
    writer.Flush(); 
    stream.Close();
}
So unfortunately I'm not sure what to tell you. Maybe Google it in the Microsoft forums.


Actipro Software Support

Posted 13 years ago by Thomas Goff
Avatar
The BOM is standard with the UTF-8 encoding in .Net. The .Net classes do have built in support for suppressing the BOM, but it's not obvious. The easiest way I've found is to build your own derived class as shown below.

Note: The base class constructor call of "base(false)" is what does the trick.

Enjoy,
Tom

using System;
using System.Collections.Generic;
using System.Text;

namespace MyNamespace.Text {
    public class NoPreambleUTF8Encoding : UTF8Encoding {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="T:UTF8NoPreambleEncoding"/> class.
        /// </summary>
        public NoPreambleUTF8Encoding()
            : base(false) {
            // No-op
        }
    
        #endregion // Constructors

        #region Properties

        /// <summary>
        /// When overridden in a derived class, gets the human-readable description of the current encoding.
        /// </summary>
        /// <returns>The human-readable description of the current <see cref="T:System.Text.Encoding"></see>.</returns>
        public override String EncodingName {
            get {
                return "UTF-8 (no preamble)";
            }
        }

        #endregion // Properties
    }
}
The latest build of this product (v2020.1 build 0400) was released 7 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.