介绍
在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.\n\nerror message: {ex.message}\n\n" + $"details:\n\n{ex.stacktrace}"); } } } } }
2.生成效果
三、使用 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.\n\nerror message: {ex.message}\n\n" + $"details:\n\n{ex.stacktrace}"); } } } } }
2.生成效果
四、一种新颖的windows窗体应用文件设计方法
这两个示例使用了一种windows窗体应用文件新的设计方法,不设计form1.cs[设计]。所有试图、控件都通过编程实现。是不是很新颖呢?你更喜欢哪一种设计方法呢?
到此这篇关于c#中openfiledialog控件的使用方法的文章就介绍到这了,更多相关c# openfiledialog控件使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论