目录
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;
}