C# 将二进制文件读入文本块

C# 将二进制文件读入文本块,c#,wpf,C#,Wpf,我想将exe或任何二进制文件内容读入C#中的文本块。我有下面的代码,但是当我开始读取文件时,应用程序被卡住了。我的代码如下: using (FileStream fs1 = new FileStream(FPath, FileMode.Open, FileAccess.Read)) { byte[] buf = new byte[1024]; int size = 0; while ((size = fs1.Read(buf, 0, buf.Length)) >

我想将exe或任何二进制文件内容读入C#中的文本块。我有下面的代码,但是当我开始读取文件时,应用程序被卡住了。我的代码如下:

using (FileStream fs1 = new FileStream(FPath, FileMode.Open, FileAccess.Read))
{
    byte[] buf = new byte[1024];
    int size = 0;

    while ((size = fs1.Read(buf, 0, buf.Length)) > 0)
    {
        Console.Write("[" + buf.Length + "/" + size + "]");
        textBox.Text += encoding.default.getstring(buf)
    }
}

请指导我如何解决此问题。

如果您只想阅读整个文件,可以使用


当您读取数据块时,这些数据块可能不代表完整的可解码字符串:有时字符需要多个字节,而这些数据块可能会以不同的数据块结尾。

您可以使用System.IO的StreamWriter类。。 这是一个样品

using( StreamReader sr = new StreamReader( FPath ) )
    {
        textBox.Text = sr.ReadToEnd( );
    }

“卡住”是什么意思?意味着用户界面只是一个想法,但是当一次阅读时,你可以直接使用
File.ReadAllText
textBox.Text=File.ReadAllText(“FPath”,Encoding.Default)using( StreamReader sr = new StreamReader( FPath ) )
    {
        textBox.Text = sr.ReadToEnd( );
    }