打开文件对话框
//创建打开文件的对象
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;
文章来源地址https://www.toymoban.com/news/detail-633703.html
文章来源:https://www.toymoban.com/news/detail-633703.html
到了这里,关于C#打开文件对话框、保存文件对话框、字体以及颜色对话框的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!