(一)打开、保存对话框
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void openBtn_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "文本文件|*.txt|视频文件|*.avi"; if (ofd.ShowDialog() == true) { string fileName = ofd.FileName; MessageBox.Show(fileName); } else { MessageBox.Show("打开失败"); } } private void saveBtn_Click(object sender, RoutedEventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "文本文件|*.txt|视频文件|*.avi"; if (sfd.ShowDialog() == true) { string fileName = sfd.FileName; MessageBox.Show(fileName); } else { MessageBox.Show("保存失败"); } }
(二)打开图片案例
使用到控件Image
private void openBtn_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "JPG|*.jpg|png|*.png"; if (ofd.ShowDialog() == true) { string fileName = ofd.FileName; image.Source = new BitmapImage(new Uri(fileName)); } else { MessageBox.Show("打开失败"); } }