C# 我想使用二进制读写器对存储在两个文本文件中的值求和,但当我运行代码时,它不会显示输出,甚至不会显示异常

C# 我想使用二进制读写器对存储在两个文本文件中的值求和,但当我运行代码时,它不会显示输出,甚至不会显示异常,c#,text-files,read-write,C#,Text Files,Read Write,问题定义如下: 我需要设计一个应用程序,将数字保存到文件中。单击“添加”按钮后,它应执行这些存储的数字的添加并显示总和。(使用TextMode) 名称空间检查 { 公共部分类Form1:Form { 公共表格1() { 初始化组件(); } 私有无效按钮1\u单击(对象发送者,事件参数e) { FileStream fs=newfilestream(@“C:\Users\MYPC\Desktop\trial.txt”,FileMode.Create); BinaryWriter bw=新的Bin

问题定义如下: 我需要设计一个应用程序,将数字保存到文件中。单击“添加”按钮后,它应执行这些存储的数字的添加并显示总和。(使用
TextMode

名称空间检查
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
FileStream fs=newfilestream(@“C:\Users\MYPC\Desktop\trial.txt”,FileMode.Create);
BinaryWriter bw=新的BinaryWriter(fs);
写入(textBox1.Text);
bw.Close();
MessageBox.Show(“保存到textfilet试用版…”);
FileStream fs1=新FileStream(@“C:\Users\MYPC\Desktop\temp.txt”,FileMode.Create);
BinaryWriter bw1=新的BinaryWriter(fs1);
bw1.写入(textBox2.Text);
bw.Close();
Show(“保存到文本文件..temp”);
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
整数和=0;
FileStream fs=newfilestream(@“C:\Users\MYPC\Desktop\trial.txt”,FileMode.Open);
BinaryReader bw=新的BinaryReader(fs);
对于(int i=0;i<1000;i++)
{
字节[]ar=新字节[500];
while(bw!=null)
{
ar[i]=(字节)bw.Read();
总和=ar[i]+ar[i+1];
}
textBox3.Text=sum.ToString();
}
}
}
}

如果我正确理解您的问题,请尝试以下方法:

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            System.IO.File.WriteAllText(@"C:\Users\MYPC\Desktop\trial.txt", textBox1.Text);
            MessageBox.Show("Save to textfilet trial...");
            System.IO.File.WriteAllText(@"C:\Users\MYPC\Desktop\temp.txt", textBox2.Text);
            MessageBox.Show("Save to textfile.. temp.");
        }
        catch (Exception ex)
        {
            //LOG>?!
            throw ex;
        }


    }

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            int file1IntVal = int.Parse(System.IO.File.ReadAllText(@"C:\Users\MYPC\Desktop\trial.txt"));
            int file2IntVal = int.Parse(System.IO.File.ReadAllText(@"C:\Users\MYPC\Desktop\temp.txt"));
            textBox3.Text = file1IntVal + file2IntVal;
        }
        catch (Exception ex)
        {
            //LOG>?!
            throw ex;
        }

    }

放置一个断点,调试你的点击处理程序,然后看看它在做什么。如果你真的想使用二进制读写器,我建议你看看下面链接中的例子:
private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            System.IO.File.WriteAllText(@"C:\Users\MYPC\Desktop\trial.txt", textBox1.Text);
            MessageBox.Show("Save to textfilet trial...");
            System.IO.File.WriteAllText(@"C:\Users\MYPC\Desktop\temp.txt", textBox2.Text);
            MessageBox.Show("Save to textfile.. temp.");
        }
        catch (Exception ex)
        {
            //LOG>?!
            throw ex;
        }


    }

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            int file1IntVal = int.Parse(System.IO.File.ReadAllText(@"C:\Users\MYPC\Desktop\trial.txt"));
            int file2IntVal = int.Parse(System.IO.File.ReadAllText(@"C:\Users\MYPC\Desktop\temp.txt"));
            textBox3.Text = file1IntVal + file2IntVal;
        }
        catch (Exception ex)
        {
            //LOG>?!
            throw ex;
        }

    }