有关winform下的对话框控件

1.判断存在

判断文件存在:

if (!File.Exists(projectPath)) return;

判断文件夹存在:

if (!Directory.Exists(projectPath)) return;

2.打开指定路径对应文件夹

  if (!Directory.Exists(projectPath)) return;
                System.Diagnostics.Process.Start("explorer.exe", projectPath);

3.用户指定文件夹(FolderBrowserDialog)

 FolderBrowserDialog folder = new FolderBrowserDialog();
 if (folder.ShowDialog() == DialogResult.OK)
  {
      tb_path.Text = folder.SelectedPath;
      tb_e=folder.SafeFileName  //获取选定对话框中的文件名和扩展名
  }

4.打开用户指定文件(OpenFileDialog)

OpenFileDialog openFileDialog=new OpenFileDialog();
    openFileDialog.InitialDirectory="c:\\";//对话框的初始目录 注意这里写路径时要用c:\\而不是c:\
    openFileDialog.Filter="文本文件|*.*|C#文件|*.cs|所有文件|*.*";//获取或设置当前文件名筛选器字符串
    openFileDialog.RestoreDirectory=true;//打开的时候记录上次打开路径
    openFileDialog.FilterIndex=1;
    openFileDialog.AddExtension = true;
    openFileDialog.CheckFileExists = true;
    openFileDialog.CheckPathExists = true;
    openFileDialog.ShowHelp = true;//是否显示帮助按钮
 if (openFileDialog.ShowDialog()==DialogResult.OK)
    {
        fName=openFileDialog.FileName;//返回选中文件的路径
        File fileOpen=new File(fName);
        isFileHaveName=true;
        richTextBox1.Text=fileOpen.ReadFile();
        richTextBox1.AppendText("");
    }

基本属性有:

属性名解释
Filter过滤要选择的文件类型,可多项
FilterIndex在对话框中选择的文件筛选器的索引,如果选第一项就设为1
Title设置文件对话框的标题
AddExtension是否自动添加默认扩展名
CheckPathExists在对话框返回之前,检查指定路径是否存在
DefaultExt默认扩展名
DereferenceLinks在从对话框返回前是否取消引用快捷方式
ShowHelp启用"帮助"按钮
ValiDateNames控制对话框检查文件名中是否不含有无效的字符或序列
InitialDirectory设置打开文件对话框的默认路径 注意这里写路径时要用c:\ \而不是c:\
RestoreDirectory打开的时候记录上次打开路径
Multiselect是否可以选择多个文件,默认是不可多选

5.保存文件(SaveFileDialog)

SaveFileDialog saveFileDialog = new SaveFileDialog();
 saveFileDialog .InitialDirectory = "d:\\";
 saveFileDialog .Filter = "txt files (*.txt)|*.txt|Excel 工作簿(*.xlsx)|*.xlsx|All files(*.*)|*>**";
 saveFileDialog .FilterIndex = 1;
 saveFileDialog .RestoreDirectory = true;
 saveFileDialog .FileName = "a1";//设置默认文件名
 saveFileDialog .DefaultExt = "txt";//设置默认格式(可以不设)
 saveFileDialog .AddExtension = true;//设置自动在文件名中添加扩展名
 saveFileDialog.CreatePrompt = true;//每次询问是否创建文件
  if (saveFileDialog .ShowDialog()==DialogResult.OK)
  {
     var extre = System.IO.Path.GetExtension(saveFileDialog .FileName);
     //判断扩展名,各自操作
     if (extre  == "txt")
     {
	      using (StreamWriter stream = new StreamWriter(saveFileDialog .FileName))
	      {              
	          stream .WriteLineAsync("需要保存的内容");
	      }
     }
     if (extre  == "xlsx")
     { 
     }

     
  }

具体导出:
https://blog.csdn.net/Yyuanyuxin/article/details/108844259

6.字体对话框(FontDialog)

 FontDialog fontDialog = new FontDialog();
 fontDialog .ShowColor = true; //是否在字体对话框中显示颜色
 if (fontDialog.ShowDialog() == DialogResult.OK)
 {
     richTextBox1.Font = fontDialog.Font;
     richTextBox1.ForeColor = fontDialog.Color;
 }

7.颜色对话框(ColorDialog)

 ColorDialog colorDialog1 = new ColorDialog();
 colorDialog1.AllowFullOpen = false;//对话框中显示自定义颜色部分
 colorDialog1.ShowHelp = true;
 if(colorDialog1.ShowDialog()==DialogResult.OK)
 {
     label1.ForeColor = colorDialog1.Color;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yyuanyuxin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值