Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get a file name from a FileDialog?

How to get a file name from a FileDialog?

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Admin887
    wrote on last edited by
    #1

    I know that there is a possibility to get the name of the file after choosing it in the FileDialog, like this:

    String str=fileDialog1.fileName()

    The String(str) includes the full address of the file. for example: str="c:\windows\system\abc.txt" but i want only the file name(abc.txt).:confused: how can i receive only the file name and not the full address? thank you G.

    S L S 3 Replies Last reply
    0
    • A Admin887

      I know that there is a possibility to get the name of the file after choosing it in the FileDialog, like this:

      String str=fileDialog1.fileName()

      The String(str) includes the full address of the file. for example: str="c:\windows\system\abc.txt" but i want only the file name(abc.txt).:confused: how can i receive only the file name and not the full address? thank you G.

      S Offline
      S Offline
      Shaahinm
      wrote on last edited by
      #2

      use safefilename like this

      DialogResult dr= openFileDialog1.ShowDialog();
      if(dr==DialogResult.OK)
      MessageBox.Show(openFileDialog1.SafeFileName);

      1 Reply Last reply
      0
      • A Admin887

        I know that there is a possibility to get the name of the file after choosing it in the FileDialog, like this:

        String str=fileDialog1.fileName()

        The String(str) includes the full address of the file. for example: str="c:\windows\system\abc.txt" but i want only the file name(abc.txt).:confused: how can i receive only the file name and not the full address? thank you G.

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        Try Path.GetFileName[^] passing the full path you get from the file dialog. Also see other static methods in the Path[^] class in case you are interested.

        Luis Alonso Ramos Intelectix Chihuahua, Mexico My Blog!

        1 Reply Last reply
        0
        • A Admin887

          I know that there is a possibility to get the name of the file after choosing it in the FileDialog, like this:

          String str=fileDialog1.fileName()

          The String(str) includes the full address of the file. for example: str="c:\windows\system\abc.txt" but i want only the file name(abc.txt).:confused: how can i receive only the file name and not the full address? thank you G.

          S Offline
          S Offline
          Sajjad Izadi
          wrote on last edited by
          #4

          hi, you can also use 'FileInfo' class to get some information of your file in the given path.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups