C#中openFileDialog控件的使用方法
介紹
在C#中,OpenFileDialog控件用于創(chuàng)建一個打開文件對話框,允許用戶選擇文件。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(); //創(chuàng)建OpenFileDialog控件openFileDialog1 openFileDialog1 = new OpenFileDialog(); //創(chuàng)建按鈕控件selectButton及添加點擊事件 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 }; //設置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 類的實例將讀取該文件,并在窗體的文本框中顯示文件內(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窗體應用文件設計方法
這兩個示例使用了一種Windows窗體應用文件新的設計方法,不設計Form1.cs[設計]。所有試圖、控件都通過編程實現(xiàn)。是不是很新穎呢?你更喜歡哪一種設計方法呢?
到此這篇關于C#中openFileDialog控件的使用方法的文章就介紹到這了,更多相關C# openFileDialog控件使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#使用SQL Dataset數(shù)據(jù)集代碼實例
今天小編就為大家分享一篇關于的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10c#之用戶定義的數(shù)據(jù)類型轉(zhuǎn)換介紹
c#允許定義自己的數(shù)據(jù)類型,這意味著需要某些工具支持在自己的數(shù)據(jù)類型間進行數(shù)據(jù)轉(zhuǎn)換。方法是把數(shù)據(jù)類型轉(zhuǎn)換定義為相關類的一個成員運算符,數(shù)據(jù)類型轉(zhuǎn)換必須聲明是隱式或者顯式,以說明怎么使用它2014-01-01C# 動態(tài)調(diào)用WebService的示例
這篇文章主要介紹了C# 動態(tài)調(diào)用WebService的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11