选择文件对话框:
private void button_chose_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog()
{
Filter = "Excel Files (*.sql)|*.sql"
};
var result = openFileDialog.ShowDialog();
if (result == true)
{
this.textblock_filename.Text = openFileDialog.FileName;
}
}
选择文件夹对话框:WPF中似乎没有打开文件夹对话框,不过可以通过winform的方法打开,调研之前需要引用System.Windows.Forms;
代码:
private void button_runScript_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog m_Dialog = new FolderBrowserDialog();
DialogResult result = m_Dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string m_Dir = m_Dialog.SelectedPath.Trim();
this.textblock_filename.Text = m_Dir;
}
全部代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MysqlScriptDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_chose_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog()
{
Filter = "Excel Files (*.sql)|*.sql"
};
var result = openFileDialog.ShowDialog();
if (result == true)
{
this.textblock_filename.Text = openFileDialog.FileName;
}
}
private void button_runScript_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog m_Dialog = new FolderBrowserDialog();
DialogResult result = m_Dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string m_Dir = m_Dialog.SelectedPath.Trim();
this.textblock_filename.Text = m_Dir;
}
}
}