Winform中使用OpenFileDialog选择文件打开并获取文件路径
場景
在Windform中進行Excel導入時往往需要先將Excel文件打開并獲取文件的路徑,這時需要使用OpenFileDialog。
OpenFileDialog
顯示一個標準對話框,提示用戶打開文件。 此類不能被繼承。
屬性
AddExtension?
獲取或設置一個值,該值指示如果用戶省略擴展名,對話框是否自動在文件名中添加擴展名。 (Inherited from FileDialog)
AutoUpgradeEnabled?
獲取或設置一個值,該值指示此 FileDialog 實例在 Windows Vista 上運行時是否應自動升級外觀和行為。 (Inherited from FileDialog)
CanRaiseEvents?
獲取一個指示組件是否可以引發事件的值。 (Inherited from Component)
CheckFileExists?
獲取或設置一個值,該值指示如果用戶指定不存在的文件名,對話框是否顯示警告。
CheckPathExists?
獲取或設置一個值,該值指示如果用戶指定不存在的路徑,對話框是否顯示警告。 (Inherited from FileDialog)
Container?
獲取 IContainer,它包含 Component。 (Inherited from Component)
CustomPlaces?
獲取此 FileDialog 實例的自定義空間的集合。 (Inherited from FileDialog)
DefaultExt?
獲取或設置默認文件擴展名。 (Inherited from FileDialog)
DereferenceLinks?
獲取或設置一個值,該值指示對話框是否返回快捷方式引用的文件的位置,或者是否返回快捷方式 (.lnk) 的位置。 (Inherited from FileDialog)
DesignMode?
獲取一個值,用以指示 Component 當前是否處于設計模式。 (Inherited from Component)
Events?
獲取附加到此 Component 的事件處理程序的列表。 (Inherited from Component)
FileName?
獲取或設置一個包含在文件對話框中選定的文件名的字符串。 (Inherited from FileDialog)
FileNames?
獲取對話框中所有選定文件的文件名。 (Inherited from FileDialog)
Filter?
獲取或設置當前文件名篩選器字符串,該字符串決定對話框的“另存為文件類型”或“文件類型”框中出現的選擇內容。 (Inherited from FileDialog)
FilterIndex?
獲取或設置文件對話框中當前選定篩選器的索引。 (Inherited from FileDialog)
InitialDirectory?
獲取或設置文件對話框顯示的初始目錄。 (Inherited from FileDialog)
Instance?
獲取應用程序的 Win32 實例句柄。 (Inherited from FileDialog)
Multiselect?
獲取或設置一個值,該值指示對話框是否允許選擇多個文件。
Options?
獲取用來初始化 FileDialog 的值。 (Inherited from FileDialog)
ReadOnlyChecked?
獲取或設置一個值,該值指示是否選定只讀復選框。
RestoreDirectory?
獲取或設置一個值,該值指示該對話框在關閉前是否將目錄還原為之前選定的目錄。 (Inherited from FileDialog)
SafeFileName?
獲取對話框中所選文件的文件名和擴展名。 文件名不包含路徑。
SafeFileNames?
獲取對話框中所有選定文件的文件名和擴展名的數組。 文件名不包含路徑。
ShowHelp?
獲取或設置一個值,該值指示文件對話框中是否顯示“幫助”按鈕。 (Inherited from FileDialog)
ShowReadOnly?
獲取或設置一個值,該值指示對話框是否包含只讀復選框。
Site?
獲取或設置 Component 的 ISite。 (Inherited from Component)
SupportMultiDottedExtensions?
獲取或設置對話框是否支持顯示和保存具有多個文件擴展名的文件。 (Inherited from FileDialog)
Tag?
獲取或設置一個對象,該對象包含控件的數據。 (Inherited from CommonDialog)
Title?
獲取或設置文件對話框標題。 (Inherited from FileDialog)
ValidateNames?
獲取或設置一個值,該值指示對話框是否只接受有效的 Win32 文件名。 (Inherited from FileDialog)
方法
CreateObjRef(Type)?
創建一個對象,該對象包含生成用于與遠程對象進行通信的代理所需的全部相關信息。 (Inherited from MarshalByRefObject)
Dispose()?
釋放由 Component 使用的所有資源。 (Inherited from Component)
Dispose(Boolean)?
釋放由 Component 占用的非托管資源,還可以另外再釋放托管資源。 (Inherited from Component)
Equals(Object)?
確定指定的對象是否等于當前對象。 (Inherited from Object)
GetHashCode()?
作為默認哈希函數。 (Inherited from Object)
GetLifetimeService()?
檢索控制此實例的生存期策略的當前生存期服務對象。 (Inherited from MarshalByRefObject)
GetService(Type)?
返回一個對象,該對象表示由 Component 或它的 Container 提供的服務。 (Inherited from Component)
GetType()?
獲取當前實例的 Type。 (Inherited from Object)
HookProc(IntPtr, Int32, IntPtr, IntPtr)?
定義要重寫的通用對話框掛鉤過程,以便向文件對話框添加特定功能。 (Inherited from FileDialog)
InitializeLifetimeService()?
獲取生存期服務對象來控制此實例的生存期策略。 (Inherited from MarshalByRefObject)
MemberwiseClone()?
創建當前 Object 的淺表副本。 (Inherited from Object)
MemberwiseClone(Boolean)?
創建當前 MarshalByRefObject 對象的淺表副本。 (Inherited from MarshalByRefObject)
OnFileOk(CancelEventArgs)?
引發 FileOk 事件。 (Inherited from FileDialog)
OnHelpRequest(EventArgs)?
引發 HelpRequest 事件。 (Inherited from CommonDialog)
OpenFile()?
打開用戶選定的具有只讀權限的文件。 該文件由 FileName 屬性指定。
OwnerWndProc(IntPtr, Int32, IntPtr, IntPtr)?
定義要重寫的所有者窗口過程,以便向通用對話框添加特定功能。 (Inherited from CommonDialog)
Reset()?
將所有屬性重新設置為其默認值。
RunDialog(IntPtr)?
指定通用對話框。 (Inherited from FileDialog)
ShowDialog()?
用默認的所有者運行通用對話框。 (Inherited from CommonDialog)
ShowDialog(IWin32Window)?
運行具有指定所有者的通用對話框。 (Inherited from CommonDialog)
ToString()?
提供此對象的字符串版本。 (Inherited from FileDialog)
事件
Disposed?
當通過調用 Dispose() 方法釋放組件時發生。 (Inherited from Component)
FileOk?
當用戶單擊文件對話框中的“打開”或 “保存”按鈕時發生。 (Inherited from FileDialog)
HelpRequest?
當用戶單擊通用對話框中的“幫助”按鈕時發生。 (Inherited from CommonDialog)
實現
新建Winform程序并拖拽一個Button,雙擊進入其點擊事件。
private void button3_Click(object sender, EventArgs e){OpenFileDialog fileDialog = new OpenFileDialog();fileDialog.Multiselect = true;fileDialog.Title = "請選擇文件";fileDialog.Filter = "所有文件(*xls*)|*.xls*"; //設置要選擇的文件的類型if (fileDialog.ShowDialog() == DialogResult.OK){localFilePath = fileDialog.FileName;//返回文件的完整路徑???????????????}MessageBox.Show(localFilePath);}效果
?
?
總結
以上是生活随笔為你收集整理的Winform中使用OpenFileDialog选择文件打开并获取文件路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Winform中使用NPOI实现导出Ex
- 下一篇: C#中DataTable使用以及对行与列