【WPF开发】超级详细的“文件选择”(附带示例工程)

在WPF(Windows Presentation Foundation)中,文件选择通常是通过使用 OpenFileDialogSaveFileDialog 类来实现的。这些类位于 Microsoft.Win32 命名空间中。

一、安装相关依赖包

1、点击"工具-NuGet包管理-管理解决方案的NuGet程序包"

2、搜索"form",并安装到自己的项目中

一定要选择微软官方的那个包

3、等待安装

4、安装完成

5、接收协议

二、代码介绍

OpenFileDialog 属性

基础属性
  • Filter:定义在对话框中显示的文件类型过滤器。
  • FileName:获取或设置在对话框中选定的文件的名称。
  • FilterIndex:获取或设置选定文件过滤器索引。
进阶属性
  • Multiselect:获取或设置一个值,该值指示对话框是否允许选择多个文件。
  • InitialDirectory:获取或设置对话框显示的初始目录。
  • Title:获取或设置对话框标题。
  • AddExtension:获取或设置一个值,该值指示在用户输入文件名但没有指定扩展名时,对话框是否自动添加扩展名。
高级属性
  • CheckFileExists:获取或设置一个值,该值指示对话框是否检查选定文件是否存在。
  • CheckPathExists:获取或设置一个值,该值指示对话框是否检查路径是否存在。
  • DefaultExt:获取或设置默认文件扩展名。
  • RestoreDirectory:获取或设置一个值,该值指示对话框关闭后,是否恢复当前目录到对话框打开前的状态。
  • ShowHelp:获取或设置一个值,该值指示对话框是否显示帮助按钮。

SaveFileDialog 属性

SaveFileDialog 类具有与 OpenFileDialog 类相似的大多数属性,以下是 SaveFileDialog 特有的属性:

基础属性
  • CreatePrompt:获取或设置一个值,该值指示如果用户指定不存在的文件,对话框是否提示创建文件。
  • OverwritePrompt:获取或设置一个值,该值指示如果用户指定已存在的文件,对话框是否提示覆盖文件。
进阶属性
  • ValidateNames:获取或设置一个值,该值指示对话框是否验证文件名。
高级属性
  • AddExtension:与 OpenFileDialog 相同,但在保存文件时,如果用户没有输入扩展名,对话框会自动添加默认扩展名。

三、代码编写

1、添加依赖

在cs文件头部添加代码

cs 复制代码
using System.Windows.Forms;

2、添加布局

XML 复制代码
    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Center" Orientation="horizontal">
        <Border Background="#ffffff" Padding="5" CornerRadius="15" Width="800">
            <Grid HorizontalAlignment="Stretch" Height="60">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="9*" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>

                <TextBox Grid.Column="0" x:Name="textboxFilePath"/>
                <Button Grid.Column="1" x:Name="botOpenFile" Content="打开文件" Click="botOpenFile_Click"/>
            </Grid>
        </Border>

    </StackPanel>

3、添加事件

cs 复制代码
        private void botOpenFile_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.Filter = "文本文件|*.mxl;*.doc;*.html;*.txt|视频文件|*.mp4;*.avi|所有文件|*.*";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            openFileDialog.Title = "打开文本文件";
            openFileDialog.Multiselect = false;

            // 显示文件对话框
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // 获取用户选择的文件路径
                textboxFilePath.Text = openFileDialog.FileName;
            }
        }

四、完整代码

xaml代码

XML 复制代码
<Window x:Class="文件选择.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:local="clr-namespace:文件选择"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Background="#838383" >

    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Center" Orientation="horizontal">
        <Border Background="#ffffff" Padding="5" CornerRadius="15" Width="800">
            <Grid HorizontalAlignment="Stretch" Height="60">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="9*" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>

                <TextBox Grid.Column="0" x:Name="textboxFilePath"/>
                <Button Grid.Column="1" x:Name="botOpenFile" Content="打开文件" Click="botOpenFile_Click"/>
            </Grid>
        </Border>

    </StackPanel>
</Window>

cs代码

cs 复制代码
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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Forms;

namespace 文件选择
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private void botOpenFile_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.Filter = "文本文件|*.mxl;*.doc;*.html;*.txt|视频文件|*.mp4;*.avi|所有文件|*.*";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            openFileDialog.Title = "打开文本文件";
            openFileDialog.Multiselect = false;

            // 显示文件对话框
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // 获取用户选择的文件路径
                textboxFilePath.Text = openFileDialog.FileName;
            }
        }

    }
}

五、效果演示

六、工程下载

通过百度网盘分享的文件:文件选择.ziphttps://pan.baidu.com/s/1-Wb0RlPJse-EfVID25JujQ

相关推荐
无证驾驶梁嗖嗖5 小时前
两台电脑网线直连SMB共享
windows·文件传输·smb
李高钢5 小时前
C# WPF Prism 进阶(三):导航(Navigation)深入
开发语言·c#·wpf
笨鸟先飞,勤能补拙20 小时前
从预测到决策:人工智能与机器学习的系统方法、工程闭环与现实边界
大数据·人工智能·windows·python·机器学习·密码学
程序员-李俞20 小时前
Mistral OCR 4真正改变的不是“识字”:文档AI正在变成Agent的数据入口
人工智能·windows·ai作画·aigc·ocr·ai编程·ai写作
C++ 老炮儿的技术栈1 天前
Qt5.9.1 Windows 完整开发环境搭建流程
开发语言·c++·windows·qt·编辑器·代码化
笨鸟先飞,勤能补拙1 天前
AI与深度学习:从原理到应用
人工智能·windows·vscode·深度学习·机器学习·网络安全·github
bkspiderx1 天前
从零开始:VS Code搭建C/C++开发环境全指南(Windows/macOS/Linux)
c语言·c++·windows·vs code·c/c++开发环境
Rudon滨海渔村1 天前
镜像转换esd转iso - VMware虚拟机如何安装或恢复(.esd)的Windows系统
windows·vmware·iso·esd
西安景驰电子1 天前
《PTP精确时间协议系列》第二篇:工程部署、调试与性能优化
运维·服务器·网络·数据库·windows·性能优化
问天_观心1 天前
零基础在windows环境下的WSL使用llamafactory(一)
人工智能·windows·python·神经网络·语言模型·github·模型蒸馏