本文介绍: 在C#中,OpenFileDialog控件用于创建一个打开文件对话框,允许用户选择文件。OpenFileDialog提供了一种简单方式来让用户选择一个多个文件,并获取用户所选文件的路径。OpenFileDialog打开文件对话框的意思,即在窗体设计中,如果需要打开本地文件,就需要用到该类。

目录

一、OpenFileDialog基本属性

二、使用 OpenFile 从筛选的选择中打开文件

1.示例源码

2.生成效果

3. 其它示例

三、使用 StreamReader 以流的形式读取文件

1.示例源码

2.生成效果

四、一种新颖的Windows窗体应用文件设计方法


        在C#中,OpenFileDialog控件用于创建一个打开文件对话框,允许用户选择文件。OpenFileDialog提供了一种简单方式来让用户选择一个或多个文件,并获取用户所选文件的路径

        OpenFileDialog打开文件对话框的意思,即在窗体设计中,如果需要打开本地文件,就需要用到该类。

一、OpenFileDialog基本属性

属性 说明
InitialDirectory 对话框的初始目录
Filter 获取设置当前文件名筛选字符串例如,“文本文件(.txt)|.txt|所有文件(.)||.”
FilterIndex 对话框中选择的文件筛选器的索引,如果选第一项就设为1
RestoreDirectory 控制对话框关闭之前是否恢复当前目录
FileName: 第一个对话框显示的文件或最后一个选取的文件
Title 显示对话框标题栏中的字符
AddExtension 是否自动添加默认扩展
CheckPathExists 对话返回之前,检查指定路径是否存在
DefaultExt 默认扩展
DereferenceLinks 在从对话返回是否取消引用快捷方式
ShowHelp 启用“帮助”按钮
ValiDateNames 控制对话检查文件名是否不含有无效字符序列

二、使用 OpenFile 从筛选的选择中打开文件

1.示例源码

//使用 OpenFile 从筛选的选择中打开文件
using System.Diagnostics;
using System.Security;

namespace WinFormsApp1
{
    public partial class OpenFileDialogForm : Form
    {
        private readonly Button selectButton;
        private readonly OpenFileDialog openFileDialog1;

        public OpenFileDialogForm()
        {
            InitializeComponent();

            //新建openFileDialog控件
            openFileDialog1 = new OpenFileDialog()
            {
                FileName = "Select a text file",      //OpenFileDialog窗体提示
                Filter = "Text files (*.txt)|*.txt",  //选择什么扩展类型的文件
                Title = "Open text file"              //OpenFileDialog窗体的抬头
            };

            //新建按钮点击事件
            selectButton = new Button()
            {
                Size = new Size(100, 20),
                Location = new Point(15, 15),
                Text = "Select file"
            };
            selectButton.Click += new EventHandler(SelectButton_Click);
            Controls.Add(selectButton);
        }

        /// <summary>
        /// 按钮点击事件应用
        /// 使用 Button 控件的 Click 事件处理程序打开包含显示本文件的筛选器的 OpenFileDialog。 
        /// 用户选择文本文件并选择“确定”后,可用 OpenFile 方法记事本中打开该文件
        /// </summary>
        private void SelectButton_Click(object? sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var filePath = openFileDialog1.FileName;
                    using Stream str = openFileDialog1.OpenFile();
                    Process.Start("notepad.exe", filePath);
                }
                catch (SecurityException ex)
                {
                    MessageBox.Show($"Security error.nnError message: {ex.Message}nn" +
                    $"Details:nn{ex.StackTrace}");
                }
            }
        }
    }
}

2.生成效果

 

3. 其它示例

         在作者的这篇文章中也有这种读取文件的示例

         写文章-CSDN创作中心  https://mp.csdn.net/mp_blog/creation/editor/134621313

三、使用 StreamReader 以流的形式读取文件

1.示例源码

//使用 StreamReader 以流的形式读取文件
using System.Security;
namespace _05_3
{
    public partial class Form1 : Form
    {
        private readonly Button selectButton;
        private readonly OpenFileDialog openFileDialog1;
        private readonly TextBox textBox1;

        public Form1()
        {
            InitializeComponent();

            //创建OpenFileDialog控件openFileDialog1
            openFileDialog1 = new OpenFileDialog();

            //创建按钮控件selectButton及添加点击事件
            selectButton = new Button
            {
                Size = new Size(100, 20),
                Location = new Point(15, 15),
                Text = "Select file"
            };
            selectButton.Click += new EventHandler(SelectButton_Click);

            //创建文本框控件textBox1
            textBox1 = new TextBox
            {
                Size = new Size(300, 300),
                Location = new Point(15, 40),
                Multiline = true,
                ScrollBars = ScrollBars.Vertical
            };

            //设置Form1表格大小
            ClientSize = new Size(330, 360);

            Controls.Add(selectButton);
            Controls.Add(textBox1);
        }

        //自定义方法
        private void SetText(string text)
        {
            textBox1.Text = text;
        }

        /// <summary>
        /// 使用 StreamReader 以流的形式读取文件
        /// 使用 Windows 窗体 Button 控件的 Click 事件处理程序通过 ShowDialog 方法打开 OpenFileDialog。
        /// 用户选择一个文件并选择“确定”后,StreamReader 类的实例读取该文件,并在窗体文本框显示文件内容。
        /// </summary>
        private void SelectButton_Click(object? sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var sr = new StreamReader(openFileDialog1.FileName);
                    SetText(sr.ReadToEnd());
                }
                catch (SecurityException ex)
                {
                    MessageBox.Show($"Security error.nnError message: {ex.Message}nn" +
                    $"Details:nn{ex.StackTrace}");
                }
            }
        }
    }
}

2.生成效果

 

四、一种新颖的Windows窗体应用文件设计方法

        这两个示例使用了一种Windows窗体应用文件新的设计方法,不设计Form1.cs[设计]。所有试图、控件都通过编程实现。是不是很新颖呢?你更喜欢哪一种设计方法呢?

原文地址:https://blog.csdn.net/wenchm/article/details/134630583

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_28508.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注