Share vb.net utf-8 without bom

buithien.duc

New member
### Cách đặt UTF-8 mà không cần BOM trong vb.net

** BOM ** (Dấu thứ tự byte) là một chuỗi các byte được sử dụng để chỉ ra mã hóa của tệp văn bản.Trong UTF-8, BOM là chuỗi `0xef, 0xbb, 0xbf`.

Khi bạn mở một tệp văn bản trong trình soạn thảo văn bản, BOM thường được tước tự động.Tuy nhiên, nếu bạn đang sử dụng VB.NET để đọc tệp văn bản, bạn có thể cần đặt rõ ràng việc mã hóa thành UTF-8 mà không cần BOM.

Để làm điều này, bạn có thể sử dụng mã sau:

`` `VBNet
Dim fs dưới dạng filestream mới ("file.txt", filemode.open, fileaccess.read)
Dim SR là Trình đọc luồng mới (FS, mã hóa.UTF8)
`` `

Tham số `mã hóa.UTF8` Chỉ định mã hóa của tệp văn bản.Theo mặc định, lớp `streamreader` sẽ tước BOM khỏi tệp.

**Thẩm quyền giải quyết:**

* [Cách đặt UTF-8 mà không BOM trong VB.NET] (Error 1001 during Window service installation - CodeProject)

### hashtags:

* #vb.net
* #UTF-8
* #BOM
* #Encoding
* #Text-FILE
=======================================
### How to Set UTF-8 Without Bom in VB.NET

**Bom** (byte order mark) is a sequence of bytes that is used to indicate the encoding of a text file. In UTF-8, the bom is the sequence `0xEF, 0xBB, 0xBF`.

When you open a text file in a text editor, the bom is usually stripped automatically. However, if you are using VB.NET to read a text file, you may need to explicitly set the encoding to UTF-8 without bom.

To do this, you can use the following code:

```vbnet
Dim fs As New FileStream("file.txt", FileMode.Open, FileAccess.Read)
Dim sr As New StreamReader(fs, Encoding.UTF8)
```

The `Encoding.UTF8` parameter specifies the encoding of the text file. By default, the `StreamReader` class will strip the bom from the file.

**Reference:**

* [How to Set UTF-8 Without Bom in VB.NET](https://www.codeproject.com/Articles/1179664/How-to-Set-UTF-8-Without-Bom-in-VB-NET)

### Hashtags:

* #vb.net
* #UTF-8
* #bom
* #Encoding
* #Text-file
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top