简体   繁体   中英

c# File ReadAllText only reads first Char

in a current development I'm stuck with a strang problem.

In a bunch of written files on my harddisk i want to read out their content and write it into a textbox. It seams very easy, but somehow i stuck in a catch:

The files are containing something like this: " <LogItem><Row Number="0"><Column Name="object_id"><Old Value="2317"/><New Value="2317"/> "

I read them with:

textBox1.Text = File.ReadAllText(filetoread);

The result of this "ReadAllTest" is only the first Char "<" everything else in not written into the textbox. Manually I can read the file with the normal Editor and this shows the complete text.

Are there any traps or limitations a haven't seen?

Best regards

It could be a problem of encoding... Rare but not impossible... Try, one at a time:

textBox1.Text = File.ReadAllText(filetoread, Encoding.Unicode);

textBox1.Text = File.ReadAllText(filetoread, Encoding.BigEndianUnicode);

textBox1.Text = File.ReadAllText(filetoread, Encoding.UTF32);

textBox1.Text = File.ReadAllText(filetoread, Encoding.UTF8);

textBox1.Text = File.ReadAllText(filetoread, Encoding.Default);

If the answer by xanatos doesn't work, try this:

using (StreamReader read = new StreamReader(filetoread))
{
   textBox1.Text = read.ReadToEnd();
}

It doesn't use File.ReadAllText() as you can see. And, this will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM