简体   繁体   中英

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). 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.

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.

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

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. Use the GetExtension method to determine the extension.

You should also look at the Filter property of the dialog. If you set it correctly "All Files" should not be shown anymore.

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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