打开文件对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "请选择要打开的文件";
openFileDialog.Multiselect = true;
openFileDialog.InitialDirectory = @"C:Usersuser1Desktop研发报告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:Usersuser1Desktop研发报告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;