C#打开文件对话框、保存文件对话框、字体以及颜色对话框

打开文件对话框

 //创建打开文件的对象
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "请选择要打开的文件";//设置对话框标题
            openFileDialog.Multiselect = true;  //设置对话框可以多选
            openFileDialog.InitialDirectory = @"C:\Users\user1\Desktop\研发报告\basler拍照研究报告";//设置对话框的初始目录
            //设置对话框的文件类型
            openFileDialog.Filter = "文本文件|*.pptx|图片文件|*.jpg|所有文件|*.*";
            openFileDialog.ShowDialog();//展示对话框
            string path = openFileDialog.FileName;//获取选中的路径
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    byte[] buffer = new byte[fs.Length];
                    int r = fs.Read(buffer, 0, buffer.Length);
                    string str = Encoding.UTF8.GetString(buffer, 0, r);
                    textBox1.Text = str;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("未选中文件");
            }

保存文件对话框

SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "请选择要保存的路径";
            saveFileDialog.InitialDirectory = @"C:\Users\user1\Desktop\研发报告\basler拍照研究报告";
            saveFileDialog.Filter = "文本文件|*.txt|所有文件|*.*";
            saveFileDialog.ShowDialog();
            string path = saveFileDialog.FileName;//获得保存文件的路径
            if (path == "")
            {
                return;
            }
            using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {

                byte[] vs = Encoding.UTF8.GetBytes(textBox1.Text);
                fileStream.Write(vs, 0, vs.Length);
            }
            MessageBox.Show("保存成功");

字体对话框

   FontDialog fontDialog = new FontDialog();
   fontDialog.ShowDialog();
   textBox2.Font = fontDialog.Font;

颜色对话框

  ColorDialog colorDialog = new ColorDialog();
  colorDialog.ShowDialog();
  textBox2.ForeColor= colorDialog.Color;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值