场景

在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中使用OpenFileDialog选择文件打开并获取文件路径_文件名_02