zoukankan      html  css  js  c++  java
  • C#用openfiledialog文件和savefileDialog打开和保存文件

    一、打开文件

    Stream myStream = null;
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.InitialDirectory = "d:\";
                openFileDialog1.Filter = "ext files (*.txt)|*.txt|All files(*.*)|*>**";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if ((myStream = openFileDialog1.OpenFile()) != null)
                        {
                            StreamReader st = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("gb2312"));
                            string str = st.ReadLine();
                            while (str != null)
                            {
                                richTextBox1.AppendText(str);
                                richTextBox1.AppendText(" ");
                                str = st.ReadLine();
                            }
                        }

                    }catch(Exception ex){
                        MessageBox.Show("打开文件出错:"+ex.Message);
                    }

    二、保存文件

    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.InitialDirectory = "d:\";
                saveFileDialog1.Filter = "ext files (*.txt)|*.txt|All files(*.*)|*>**";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                DialogResult dr = saveFileDialog1.ShowDialog();
                if (dr == DialogResult.OK && saveFileDialog1.FileName.Length > 0)
                {
                    richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
                    MessageBox.Show("存储文件成功!","保存文件");
                }

    三、颜色对话框

     ColorDialog colorDialog1 = new ColorDialog();
                colorDialog1.AllowFullOpen = false;
                colorDialog1.Color = richTextBox1.ForeColor;
                colorDialog1.ShowHelp = true;
                if(colorDialog1.ShowDialog()==DialogResult.OK)
                {
                    richTextBox1.ForeColor = colorDialog1.Color;

                 }

    四、字体对话框

     FontDialog fontDialog = new FontDialog();
                fontDialog.Font = richTextBox1.Font;
                fontDialog.Color = richTextBox1.ForeColor;
                if (fontDialog.ShowDialog() != DialogResult.Cancel)
                {
                    richTextBox1.Font = fontDialog.Font;
                    richTextBox1.ForeColor = fontDialog.Color;
                }

  • 相关阅读:
    php数据过滤函数与方法示例【转载】
    MySQL 数据类型 详解 (转载)
    【总结整理】产品经理-电台
    【总结整理】职业选择
    【总结整理】什么样的产品是好产品
    【总结整理】微信产品-张小龙-PM学习总结
    Arcgis engine编程报错查询(转)
    ArcEngine开发遇到的问题(转)
    【总结整理】OpenLayers项目分析,OpenLayers中的图层,GeoServer发布wms服务--实验(转)
    带你剖析WebGis的世界奥秘----Geojson数据加载(高级)(转)
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/6912253.html
Copyright © 2011-2022 走看看