Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

How to create a random Unicode or UTF-8 file in vb.net?

I have a problem with the encoding of my files. These are direct access files. The TextBoxes that feed these files are able to display all characters, in Polish, Ukrainian, etc. and of course in my language, in French, with all the accented characters.

On the other hand, thinking that they have to be encoded in Unicode, while they are created in utf8, well I think (I'm not sure at all), after saving the data, the exotic accents disappear ! Only the French accents remain!

Example : Ró?a Czacka. Which gives me Róza Czacka once recorded!

The code I use to save my files:

Nbr = FreeFile()

FileOpen(Nbr, OuvrirFichier, OpenMode.Random, OpenAccess.ReadWrite, _
         OpenShare.LockWrite, Len(bibliotheque))

enreg = FileLen(OuvrirFichier)  Len(bibliotheque) 

With bibliotheque
    .Title = TextBox2.Text
    .Name = TextBox1.Text
    .Charge = TextBox4.Text
    .Institute = TextBox15.Text
    .Celebration = TextBox5.Text
    .Birth = TextBox7.Text
    .Death = TextBox9.Text
    .Otherparties = TextBox13.Text
    .Othernames = TextBox18.Text
    .Venerable = TextBox23.Text
    .Beatified = TextBox24.Text
    .Canonized = TextBox25.Text
    .Heading = TextBox26.Text
    .Patron = TextBox28.Text
    .Link = TextBox29.Text
    .Biography = TextBox31.Text
    .Image = TextBox33.Text
End With

FilePut(Nbr, bibliotheque, enreg)
FileClose(Nbr)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, the functions you're calling are so dated that many people will not recognize them. In fact, I had to look up a few of them myself in order to understand what you were doing. It is recommended to use the methods in the Microsoft.VisualBasic.FileIO.FileSystem class. If you need more flexibility, you can look at the classes in the System.IO.

In your case, you appear to be trying to save a binary object into a file and for that you can use the BinaryFormatter. Here is a sample code slightly modified from what is provided on the Microsoft Docs page for the BinaryFormatter class.

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Module Module1

    Sub Main()
        Dim bibliotheque = New With {
            .Title = "Title",
            .Name = "NameOf",
            .Charge = "Charge" ' add other initialization code
        }

        Using fs = New FileStream("C:folderfile.dat", FileMode.Create)
            Dim bf = New BinaryFormatter()
            bf.Serialize(fs, bibliotheque)
        End Using
    End Sub

End Module

You can also look at the same page for the BinaryFormatter for a sample on how to deserialize (or open) the saved object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...