C#中openFileDialog控件的使用方法
介紹
在C#中,OpenFileDialog控件用于創(chuàng)建一個(gè)打開(kāi)文件對(duì)話框,允許用戶選擇文件。OpenFileDialog提供了一種簡(jiǎn)單的方式來(lái)讓用戶選擇一個(gè)或多個(gè)文件,并獲取用戶所選文件的路徑。
OpenFileDialog是打開(kāi)文件對(duì)話框的意思,即在窗體設(shè)計(jì)中,如果需要打開(kāi)本地文件,就需要用到該類。
一、OpenFileDialog基本屬性
屬性 | 說(shuō)明 |
InitialDirectory | 對(duì)話框的初始目錄 |
Filter | 獲取或設(shè)置當(dāng)前文件名篩選器字符串,例如,“文本文件(.txt)|.txt|所有文件(.)||.” |
FilterIndex | 在對(duì)話框中選擇的文件篩選器的索引,如果選第一項(xiàng)就設(shè)為1 |
RestoreDirectory | 控制對(duì)話框在關(guān)閉之前是否恢復(fù)當(dāng)前目錄 |
FileName: | 第一個(gè)在對(duì)話框中顯示的文件或最后一個(gè)選取的文件 |
Title | 將顯示在對(duì)話框標(biāo)題欄中的字符 |
AddExtension | 是否自動(dòng)添加默認(rèn)擴(kuò)展名 |
CheckPathExists | 在對(duì)話框返回之前,檢查指定路徑是否存在 |
DefaultExt | 默認(rèn)擴(kuò)展名 |
DereferenceLinks | 在從對(duì)話框返回前是否取消引用快捷方式 |
ShowHelp | 啟用"幫助"按鈕 |
ValiDateNames | 控制對(duì)話框檢查文件名中是否不含有無(wú)效的字符或序列 |
二、使用 OpenFile 從篩選的選擇中打開(kāi)文件
1.示例源碼
//使用 OpenFile 從篩選的選擇中打開(kāi)文件 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", //選擇什么擴(kuò)展名類型的文件 Title = "Open text file" //OpenFileDialog窗體的抬頭 }; //新建按鈕及點(diǎn)擊事件 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> /// 按鈕點(diǎn)擊事件應(yīng)用 /// 使用 Button 控件的 Click 事件處理程序打開(kāi)包含僅顯示文本文件的篩選器的 OpenFileDialog。 /// 用戶選擇文本文件并選擇“確定”后,可用 OpenFile 方法在記事本中打開(kāi)該文件 /// </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(); //創(chuàng)建OpenFileDialog控件openFileDialog1 openFileDialog1 = new OpenFileDialog(); //創(chuàng)建按鈕控件selectButton及添加點(diǎn)擊事件 selectButton = new Button { Size = new Size(100, 20), Location = new Point(15, 15), Text = "Select file" }; selectButton.Click += new EventHandler(SelectButton_Click); //創(chuàng)建文本框控件textBox1 textBox1 = new TextBox { Size = new Size(300, 300), Location = new Point(15, 40), Multiline = true, ScrollBars = ScrollBars.Vertical }; //設(shè)置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 事件處理程序通過(guò) ShowDialog 方法打開(kāi) OpenFileDialog。 /// 用戶選擇一個(gè)文件并選擇“確定”后,StreamReader 類的實(shí)例將讀取該文件,并在窗體的文本框中顯示文件內(nèi)容。 /// </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窗體應(yīng)用文件設(shè)計(jì)方法
這兩個(gè)示例使用了一種Windows窗體應(yīng)用文件新的設(shè)計(jì)方法,不設(shè)計(jì)Form1.cs[設(shè)計(jì)]。所有試圖、控件都通過(guò)編程實(shí)現(xiàn)。是不是很新穎呢?你更喜歡哪一種設(shè)計(jì)方法呢?
到此這篇關(guān)于C#中openFileDialog控件的使用方法的文章就介紹到這了,更多相關(guān)C# openFileDialog控件使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用SQL Dataset數(shù)據(jù)集代碼實(shí)例
今天小編就為大家分享一篇關(guān)于的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10深入淺析C#?11?對(duì)?ref?和?struct?的改進(jìn)
這篇文章主要介紹了C#?11?對(duì)?ref?和?struct?的改進(jìn),有了這些基礎(chǔ)設(shè)施,開(kāi)發(fā)者們將能輕松使用安全的方式來(lái)編寫沒(méi)有任何堆內(nèi)存開(kāi)銷的高性能代碼,需要的朋友可以參考下2022-04-04c#之用戶定義的數(shù)據(jù)類型轉(zhuǎn)換介紹
c#允許定義自己的數(shù)據(jù)類型,這意味著需要某些工具支持在自己的數(shù)據(jù)類型間進(jìn)行數(shù)據(jù)轉(zhuǎn)換。方法是把數(shù)據(jù)類型轉(zhuǎn)換定義為相關(guān)類的一個(gè)成員運(yùn)算符,數(shù)據(jù)類型轉(zhuǎn)換必須聲明是隱式或者顯式,以說(shuō)明怎么使用它2014-01-01C#實(shí)現(xiàn)復(fù)雜XML的序列化與反序列化
這篇文章主要介紹了C#實(shí)現(xiàn)復(fù)雜XML的序列化與反序列化的方法,是非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下2014-09-09C# 動(dòng)態(tài)調(diào)用WebService的示例
這篇文章主要介紹了C# 動(dòng)態(tài)調(diào)用WebService的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11