如何保存和加载.bin文件.

问题描述:

public Form1()
 {
     InitializeComponent();

 }
 double[] dnum;
 int total;
 Random rNumber = new Random();
 private void BTN_generate_Click(object sender, EventArgs e)
 {
     total = rNumber.Next(50, 1001);
     dnum = new double[total];

     for (int x = 0; x < dnum.GetLength(0); x++)
     {
         dnum[x] = rNumber.NextDouble() * 100.0;
         textBox1.AppendText(dnum[x].ToString("F2") + ",");
     }
     LBL_sum.Text = dnum.Sum().ToString("F2");
 }

 private void BTN_save_Click(object sender, EventArgs e)
 {
     DialogResult dr;
     dr = saveFileDialog1.ShowDialog();
     if (dr == DialogResult.OK)
     {
         try
         {
             FileStream fs = new FileStream(saveFileDialog1.FileName.ToString(),
                 FileMode.Open, FileAccess.Write);
             BinaryWriter bw = new BinaryWriter(fs);

             foreach (double dArray in dnum)
                 bw.Write(dArray);

             bw.Close();
             fs.Close();
         }
         catch
         {
             MessageBox.Show("File save error.");
         }
     }
 }

 private void BTN_load_Click(object sender, EventArgs e)
 {
     DialogResult dr;
     dr = openFileDialog1.ShowDialog();
     if (dr == DialogResult.OK)
     {
         try
         {
             textBox1.Clear();
             long ldoubles = 0;
             FileStream fs = new FileStream(openFileDialog1.FileName.ToString(),
                 FileMode.Open, FileAccess.Read);
             BinaryReader br = new BinaryReader(fs);
             ldoubles = fs.Length / sizeof(int) / 2;
             dnum = new double[ldoubles];
             for (int x = 0; x < ldoubles; x++)
             {
                 dnum[x] = br.ReadDouble();
                 textBox1.Text += dnum[x].ToString("F2") + ",";
             }
             LBL_sum.Text = dnum.Sum().ToString("F2");
         }
         catch
         {
             MessageBox.Show("Load Error.");
         }
     }


不知道为什么吗?
我无法保存和加载文件...必须将其保存为.bin文件...
savefiledialog和openfiledialog中的过滤器是
二进制文件(* .bin)|(*.*)|所有文件(*.*)|(*.*)"


don''t know why???
i can''t save and load the file...have to save it .bin file...
the filter in savefiledialog and openfiledialog is
"Binary files (*.bin)|(*.*)| All files (*.*)|(*.*)"

更改此

Change this

FileStream fs = new FileStream(saveFileDialog1.FileName.ToString(),
    FileMode.Open, FileAccess.Write);



对此



to this

FileStream fs = new FileStream(saveFileDialog1.FileName,
    FileMode.OpenOrCreate, FileAccess.Write);



在BTN_save_Click事件处理程序中.

注意从"FileMode.Open"到"FileMode.OpenOrCreate"的更改

同样对于打开/保存对话框过滤器,请更改此

二进制文件(* .bin)|(*.*)|所有文件(*.*)|(*.*)"

对此

二进制文件(* .bin)| * .bin |所有文件(*.*)| *.*"

哦,别忘了在保存和加载之间清除文本框"textBox1.Clear()"



in the BTN_save_Click event handler.

Note the change from "FileMode.Open" to "FileMode.OpenOrCreate"

Also for the open/save dialog filters change this

"Binary files (*.bin)|(*.*)| All files (*.*)|(*.*)"

to this

"Binary files (*.bin)|*.bin|All files (*.*)|*.*"

Oh and don''t forget to clear your textbox between saves and loads "textBox1.Clear()"