wpf-选择文件,文件夹

选择文件

<TextBlock VerticalAlignment="Center" Margin="4,0">测量项监控目录:</TextBlock>
<TextBox x:Name="tb_monitor_path" Width="500" VerticalAlignment="Center" Text="{Binding Path=MonitorPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Button x:Name="btn_browse" VerticalAlignment="Center" Margin="4,0" Click="btn_browse_Click">浏 览</Button>


var openFileDialog = new Microsoft.Win32.OpenFileDialog()
{
Filter = "Excel Files (*.sql)|*.sql|All files (*.*)|*.*"
};
var result = openFileDialog.ShowDialog();
if (result == true)
{
this.textblock_filename.Text = openFileDialog.FileName;
}

选择文件夹

.net core

// 项目名称上 右键菜单- Edit project file
// 项目里面添加 <UseWindowsForms>true</UseWindowsForms>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

// 代码如下
private void btn_monitor_browse_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = fbd.ShowDialog();

if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
tb_monitor_path.Text = fbd.SelectedPath.Trim();
App.db.Update_model<Model_config>(App.db.m_config[0]);
}

.net framework

项目右键 add reference- Assemblies -System.Windows.Forms