返回首页

当按下"保存"按钮,,SaveDialog将被用来选择一个二进制文件使用的文件名。斌使用的默认扩展名,并为二进制文件(*。彬)和所有文件(*。*)的过滤器。数组的内容到选定的文件保存,使用MessageBox显示遇到的任何错误。

Load按钮被按下时,,OpenDialog将被用来选择一个可用于读取二进制文件的文件名。斌使用的默认扩展名,并为二进制文件(*。彬)和所有文件(*。*)的过滤器。读取数组的内容从选定的文件,使用MessageBox显示遇到的任何错误。请注意,必须建立正确的长度,从文件中读双打双打阵列。清除以前的内容,然后显示多行文本框,上面显示的格式读入阵列双打。显示值的总和,在从文件中读取。

我不知道为什么负载不能正常工作...

    public partial class Form1 : Form

    {

        Random rNumber = new Random();

 

        public Form1()

        {

            InitializeComponent();

 

        }

 

        private void BTN_generate_Click(object sender, EventArgs e)

        {

            int total = rNumber.Next(50, 1001);

            List<double> numbers = new List<double>();

 

            for (int i = 0; i < total; i++)

 

                numbers.Add(Math.Round(0.0 + rNumber.NextDouble() * 100.0, 2));

            textBox1.Text = String.Join(", ", numbers.Select(s => s.ToString()).ToArray());

 

            double dtotal = numbers.Sum();

            LBL_sum.Text = dtotal.ToString("F");

        }

 

        private void BTN_save_Click(object sender, EventArgs e)

        {

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "Binary Files (*.bin)|*.*|All fIles (*.*)|*.*";

            saveFileDialog1.ShowDialog();

            FileStream fs;

 

            try

            {

                fs = new FileStream("Test.dat", FileMode.Create, FileAccess.Write);

                BinaryWriter bw = new BinaryWriter(fs);

                bw.Write("C:");

                bw.Close();

                fs.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "ICA 9");

            }

 

        }

 

        private void BTN_load_Click(object sender, EventArgs e)

        {

            

            openFileDialog1.InitialDirectory = "c:\\";

            openFileDialog1.Filter = "Binary Files (*.bin)|*.*|All fIles (*.*)|*.*";

            long iNumInts = 0;

            int[] iArray = null;

 

            try

            {

                FileStream fs = new FileStream("Test.dat", FileMode.Open, FileAccess.Read);

                BinaryReader br = new BinaryReader(fs);

                iNumInts = fs.Length / sizeof(int);

                iArray = new int[iNumInts];

                for (int i = 0; i < iNumInts; i++)

                    iArray[i] = br.ReadInt32();

                br.Close();

                fs.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "BinaryReaderExample");

            }

        }

    }</double></double>
:

回答

评论会员:k 时间:2