简体   繁体   English

VB.NET中打开的文件的文件扩展名

[英]File Extension of opened file in VB.NET

I have a small requirement and that is as follows: 我的要求不高,如下所示:

I have opened a file using the "openFileDialog" provision and i have set the filer as (*.txt). 我已经使用“ openFileDialog”条款打开了一个文件,并将文件管理器设置为(* .txt)。 Now when the openFileDialog is displayed, only the test files are visible, but the user can also select "All Files" and choose any other file type. 现在,当显示openFileDialog时,仅测试文件可见,但用户也可以选择“所有文件”并选择任何其他文件类型。

Now what is require is that, if a file type other than .txt is selected by the user, i want to display an error message. 现在需要的是,如果用户选择了.txt以外的文件类型,我想显示一条错误消息。

So is there any provision by which i can get to know the file type that is selected by the user. 因此,有什么规定可以让我了解用户选择的文件类型。

Regards, George 问候,乔治

Look at http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx 看看http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx

Dim fileName As String = "C:\mydir.old\myfile.ext"
Dim extension As String

extension = Path.GetExtension(fileName)
Console.WriteLine("GetExtension('{0}') returns '{1}'", fileName, extension)

You can use the FileOK event to show the message box while the dialog is still open. 对话框仍打开时,可以使用FileOK事件显示消息框。 Use the GetExtension method to determine the extension. 使用GetExtension方法确定扩展名。

You should also look at the Filter property of the dialog. 您还应该查看对话框的Filter属性。 If you set it correctly "All Files" should not be shown anymore. 如果设置正确,则不再显示“所有文件”。

Example: 例:

dlg.Filter = "Test-Files (*.txt)|*.txt"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM