C#之文件选择器和文件保存器

 //序列化二维数组导出文件
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //文件路径
                string path = string.Empty; 
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "绘图文件(*.wk4)|*.wk4"; ;
                if (save.ShowDialog() == DialogResult.OK)
                    path = save.FileName;
                if (path != string.Empty)
                {
                    //true为创建新文件,false为不创建
                    StreamWriter sw = new StreamWriter(path, true);    
                    for (int i = 0; i < 6000; i++)
                    {
                        for (int j = 0; j < 6000; j++)
                            sw.Write(arrSave[i, j] + " ");      
                        sw.WriteLine();
                    }
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();

                    /**
                     * 存在问题
                     */
                    //创建二进制序列化器  
                    //BinaryFormatter binFormat = new BinaryFormatter();
                    //binFormat.Serialize(fStream, arrSave);
                    //fStream.Close();
                    //fStream.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //反序列化导入文件
        private void but_import_Click(object sender, EventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();

            //清屏
            g.Clear(Color.WhiteSmoke);
            OpenFileDialog fileDialog = new OpenFileDialog();
            //限定扩展
            fileDialog.Filter = "绘图文件(*.wk4)|*.wk4";
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string path = fileDialog.FileName;
                MessageBox.Show("已选择文件:" + path, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                var file = File.Open(path , FileMode.Open);
                List txt = new List();
                using (var stream = new StreamReader(file))
                {
                    while (!stream.EndOfStream)
                    {
                        txt.Add(stream.ReadLine());
                    }
                }
                var array = new String[6000, 6000];
                var line = 0;
                txt.ForEach(t =>
                {
                    var row = 0;
                    t.Split(',').ToList().ForEach(p =>
                    {
                        array.SetValue(p, line, row);
                        row++;
                    });
                    line++;
                });
                file.Close();



                /**
                 * 存在问题
                 * 
                 */
                //try
                //{
                //    //反序列化
                //    Stream fStream = new FileStream(file, FileMode.Create, FileAccess.ReadWrite);
                //    BinaryFormatter binFormat = new BinaryFormatter();
                //    arrSave = (int[,])binFormat.Deserialize(fStream);//反序列化对象
                //}
                //catch (Exception e1)
                //{
                //    MessageBox.Show("导入失败:" + file, "文件错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}

你可能感兴趣的:(C#)