程序集:
System.Windows.Forms.dll
顯示一個標準對話框,提示用戶打開文件。 此類不能被繼承。
C#複製
public sealed class OpenFileDialog : System.Windows.Forms.FileDialog
繼承
OpenFileDialog
示例
下面的代碼示例創建一個OpenFileDialog, 設置多個屬性以定義文件擴展名篩選器和對話框行爲, 並CommonDialog.ShowDialog使用方法顯示對話框。 該示例要求在窗體上Button放置了一個並對其添加了System.IO命名空間的引用。
C#複製
var fileContent = string.Empty;
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
//Read the contents of the file into a stream
var fileStream = openFileDialog.OpenFile();
using (StreamReader reader = new StreamReader(fileStream))
{
fileContent = reader.ReadToEnd();
}
}
}
MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
註解
此類可用於檢查文件是否存在並打開它。 ShowReadOnly屬性確定是否在對話框中出現只讀複選框。 ReadOnlyChecked屬性指示是否選中只讀複選框。
此類的大部分核心功能都可在FileDialog類中找到。
在從右到左的操作系統上, 設置包含窗體RightToLeft的屬性以RightToLeft.Yes設置對話框的文件名、打開和取消按鈕的外觀。 如果該屬性未設置爲RightToLeft.Yes, 則改爲使用英語文本。
如果要使用戶能夠選擇文件夾而不是文件, 請FolderBrowserDialog改用。
構造函數
OpenFileDialog() |
初始化 OpenFileDialog 類的實例。 |
屬性
AddExtension |
獲取或設置一個值,該值指示如果用戶省略擴展名,對話框是否自動在文件名中添加擴展名。 (繼承自 FileDialog) |
AutoUpgradeEnabled |
獲取或設置一個值,該值指示此 FileDialog 實例在 Windows Vista 上運行時是否應自動升級外觀和行爲。 (繼承自 FileDialog) |
CanRaiseEvents |
獲取一個指示組件是否可以引發事件的值。 (繼承自 Component) |
CheckFileExists |
獲取或設置一個值,該值指示如果用戶指定不存在的文件名,對話框是否顯示警告。 |
CheckPathExists |
獲取或設置一個值,該值指示如果用戶指定不存在的路徑,對話框是否顯示警告。 (繼承自 FileDialog) |
Container |
獲取 IContainer,它包含 Component。 (繼承自 Component) |
CustomPlaces |
獲取此 FileDialog 實例的自定義空間的集合。 (繼承自 FileDialog) |
DefaultExt |
獲取或設置默認文件擴展名。 (繼承自 FileDialog) |
DereferenceLinks |
獲取或設置一個值,該值指示對話框是否返回快捷方式引用的文件的位置,或者是否返回快捷方式 (.lnk) 的位置。 (繼承自 FileDialog) |
DesignMode |
獲取一個值,用以指示 Component 當前是否處於設計模式。 (繼承自 Component) |
Events |
獲取附加到此 Component 的事件處理程序的列表。 (繼承自 Component) |
FileName |
獲取或設置一個包含在文件對話框中選定的文件名的字符串。 (繼承自 FileDialog) |
FileNames |
獲取對話框中所有選定文件的文件名。 (繼承自 FileDialog) |
Filter |
獲取或設置當前文件名篩選器字符串,該字符串決定對話框的“另存爲文件類型”或“文件類型”框中出現的選擇內容。 (繼承自 FileDialog) |
FilterIndex |
獲取或設置文件對話框中當前選定篩選器的索引。 (繼承自 FileDialog) |
InitialDirectory |
獲取或設置文件對話框顯示的初始目錄。 (繼承自 FileDialog) |
Instance |
獲取應用程序的 Win32 實例句柄。 (繼承自 FileDialog) |
Multiselect |
獲取或設置一個值,該值指示對話框是否允許選擇多個文件。 |
Options |
獲取用來初始化 FileDialog 的值。 (繼承自 FileDialog) |
ReadOnlyChecked |
獲取或設置一個值,該值指示是否選定只讀複選框。 |
RestoreDirectory |
獲取或設置一個值,該值指示該對話框在關閉前是否將目錄還原爲之前選定的目錄。 (繼承自 FileDialog) |
SafeFileName |
獲取對話框中所選文件的文件名和擴展名。 文件名不包含路徑。 |
SafeFileNames |
獲取對話框中所有選定文件的文件名和擴展名的數組。 文件名不包含路徑。 |
ShowHelp |
獲取或設置一個值,該值指示文件對話框中是否顯示“幫助”按鈕。 (繼承自 FileDialog) |
ShowReadOnly |
獲取或設置一個值,該值指示對話框是否包含只讀複選框。 |
Site | (繼承自 Component) |
SupportMultiDottedExtensions |
獲取或設置對話框是否支持顯示和保存具有多個文件擴展名的文件。 (繼承自 FileDialog) |
Tag |
獲取或設置一個對象,該對象包含控件的數據。 (繼承自 CommonDialog) |
Title |
獲取或設置文件對話框標題。 (繼承自 FileDialog) |
ValidateNames |
獲取或設置一個值,該值指示對話框是否只接受有效的 Win32 文件名。 (繼承自 FileDialog) |
方法
事件
Disposed |
當通過調用 Dispose() 方法釋放組件時發生。 (繼承自 Component) |
FileOk |
當用戶單擊文件對話框中的“打開”或 “保存”按鈕時發生。 (繼承自 FileDialog) |
HelpRequest |
當用戶單擊通用對話框中的“幫助”按鈕時發生。 (繼承自 CommonDialog) |
適用於
.NET Core
3.0
.NET Framework
4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1