Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1046
  • Last Modified:

how do I get the short file name from the openfile dialog

I need to get just the filename of a file without the path after I open it  in vb.net
how do I do that?
0
Dov_B
Asked:
Dov_B
  • 3
4 Solutions
 
jpaulinoCommented:
You can use FileInfo() to get the filename only
 
Example:

        Dim fileDialog As New OpenFileDialog
        fileDialog.ShowDialog()
        Debug.WriteLine(New IO.FileInfo(fileDialog.FileName).Name)

Open in new window

0
 
rheitzmanCommented:
I assume you are not talking about the 8.3 name but just the filename:

x= My.Computer.FileSystem.GetName(OpenFileDialog1.FileName())

IO.FileInfo above works too but it is hard to use IMO, mainly because Intellisense doesn't work.

For some reason VB.Net has more than one way at the data - neither is very VB like.
0
 
mkosbieCommented:
Intellisense works fine with FileInfo, you just have to use it properly.  Either import the System.IO Namespace, or use System.IO.FileInfo to refer to the class.

As for having "multiple ways at the data" it's because the two classes are for very different tasks.  A FileInfo object represents an actual file on the file system and can be manipulated (renamed, deleted, read, etc).  My.Computer.FileSystem.GetName is JUST A PARSING function.  It reads the path and parses the name out of it.

If all you want is the name, definitely use My.Computer.FileSystem.GetName.  It has way less overhead and is designed for this task.
0
 
jpaulinoCommented:
If you use VS2008 you can also use:
 

        Dim fileDialog As New OpenFileDialog
        fileDialog.ShowDialog()
        Debug.WriteLine(fileDialog.SafeFileName)

Open in new window

0
 
jpaulinoCommented:
>> mainly because Intellisense doesn't work.
Intellisense works fine!
 
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now