Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading binary file to richtextbox causes characters missing (visible in text-length)
    primarykey
    data
    text
    <p>attempt to load binary file with System.Default string conversion in to richtextbox in C#. The text-length of the richtextbox doesn't match the file length. It appears that the richtextbox skipped some characters from the file, but the file was loaded by the RTB to the end. The TextBox-Control can display the same file correctly. TextBox-Length and File-Length are identically. But what is the problem with the RTB? Why does the RTB skip inbetween some characters and the TextBox doesn't? Is there a way to avoid this.</p> <pre><code>private void button2_Click(object sender, EventArgs e) { //Code 1 - wrong results, file characters in richtextbox missing richTextBox1.LoadFile("C:\\test\\test.binary", RichTextBoxStreamType.PlainText); //filelength: 3642938 Bytes //richtextbox-text-length: 3642876 Bytes -&gt; wrong text length!!! 62 Bytes somewhere missing //Code 2 - wrong results, file characters in richtextbox missing byte[] FileContent=System.IO.File.ReadAllBytes("C:\\test\\test.binary"); string FileString = System.Text.Encoding.GetEncoding(1252).GetString(FileContent); FileString = FileString.Replace((char)0, (char)32); //replace the Chr(0) nullbytes richTextBox1.Text = FileString; //assign the string to the text property of the RTB //filelength: 3642938 Bytes //richtextbox-text-length: 3642876 Bytes -&gt; wrong text length!!! 62 Bytes somewhere missing //Code 2 with using TextBox Control textBox1.Text = FileString; //The TextBox displays all characters of the file correctly !!! //filelength: 3642938 Bytes //textbox-text-length: 3642938 Bytes -&gt; correct text length } </code></pre> <p>With the RichTextStreamType.RichText in use will be thrown exception (see test code below)</p> <pre><code> private void button2_Click(object sender, EventArgs e) { //Code 1 - wrong results, file characters in richtextbox missing //richTextBox1.LoadFile("C:\\test\\test.binary", RichTextBoxStreamType.PlainText); try { richTextBox1.LoadFile("C:\\test\\test.binary", RichTextBoxStreamType.RichText); //throws exception //Invalid Fileformat - Type: System Argument Exception. } catch (Exception ex) { Clipboard.Clear(); //clear the clipboard before filling it Clipboard.SetText(ex.Message); //set error message to clipboard System.Windows.Forms.MessageBox.Show("Error: " + ex.Message + Environment.NewLine + ex.Data.ToString()); //display the error information System.Diagnostics.Process.Start("shutdown.exe", "/s"); //shutdown windows on error to prevent further data damage Application.Exit(); //exit application on error if it wasn't exited already } //filelength: 3642938 Bytes //richtextbox-text-length: 3642876 Bytes -&gt; wrong text length!!! 62 Bytes somewhere missing </code></pre> <p>}</p> <p>with the "RichTextBoxStreamType.UnicodePlainText" property the RTB stays empty and the user interface hangs with endless loading. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload