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. Help need in OpenFileDialog

Help need in OpenFileDialog

Scheduled Pinned Locked Moved C#
helpquestion
10 Posts 6 Posters 0 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
    Ashraj1982
    wrote on last edited by
    #1

    Hi, using openfiledialog to select a particular file and store the selected file name in a string it sores the filename with path so is there any way to store only the file name not the whole path? Thanks

    S N 2 Replies Last reply
    0
    • A Ashraj1982

      Hi, using openfiledialog to select a particular file and store the selected file name in a string it sores the filename with path so is there any way to store only the file name not the whole path? Thanks

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Path.GetFileName Method


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      A 1 Reply Last reply
      0
      • S Stefan Troschuetz

        Path.GetFileName Method


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        A Offline
        A Offline
        Ashraj1982
        wrote on last edited by
        #3

        There is no method called getfilename !! see I have this below OpenFileDialog select_file = new OpenFileDialog(); select_file.Title = "selected file"; select_file.Filter = "(*.txt) | *.txt"; if (select_file.ShowDialog() != DialogResult.Cancel) { file = select_file.FileName; // stores whole path with file name } but i want only selected file name in file not the path .

        G J N S 4 Replies Last reply
        0
        • A Ashraj1982

          There is no method called getfilename !! see I have this below OpenFileDialog select_file = new OpenFileDialog(); select_file.Title = "selected file"; select_file.Filter = "(*.txt) | *.txt"; if (select_file.ShowDialog() != DialogResult.Cancel) { file = select_file.FileName; // stores whole path with file name } but i want only selected file name in file not the path .

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          How will you know where the file is, then? Use the methods in the System.IO.Path class to get different parts of a complete file path. --- b { font-weight: normal; }

          1 Reply Last reply
          0
          • A Ashraj1982

            There is no method called getfilename !! see I have this below OpenFileDialog select_file = new OpenFileDialog(); select_file.Title = "selected file"; select_file.Filter = "(*.txt) | *.txt"; if (select_file.ShowDialog() != DialogResult.Cancel) { file = select_file.FileName; // stores whole path with file name } but i want only selected file name in file not the path .

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Did you actually click on the link you were provided with? Changing just 1 line of your code would get you what you want! Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

            1 Reply Last reply
            0
            • A Ashraj1982

              There is no method called getfilename !! see I have this below OpenFileDialog select_file = new OpenFileDialog(); select_file.Title = "selected file"; select_file.Filter = "(*.txt) | *.txt"; if (select_file.ShowDialog() != DialogResult.Cancel) { file = select_file.FileName; // stores whole path with file name } but i want only selected file name in file not the path .

              N Offline
              N Offline
              NaNg15241
              wrote on last edited by
              #6

              well... I use a little longer way, but I think there is a shorter way, but I don't know it... so here is my way: file = select_file.FileName.Substring(select_file.FileName.LastIndexOf("\\") + 1, select_file.FileName.Length - select_file.FileName.LastIndexOf("\\") - 5); // -5 = -4 - 3 letters of ending (you can cancel that if you want the ending) and 1 dot, and -1 - for the +1 we do in the first parameter to cancel the \ in the start. ---- modified ---- the other way is better (as they told you here). if you didn't see the examples so look here: file = System.IO.Path.GetFileName(select_file.FileName); // Will retrive the file's name with extension file = System.IO.Path.GetFileNameWithoutExtension(select_file.FileName); // Will retrive the file's name with out extension -- modified at 4:12 Friday 12th May, 2006

              S 1 Reply Last reply
              0
              • N NaNg15241

                well... I use a little longer way, but I think there is a shorter way, but I don't know it... so here is my way: file = select_file.FileName.Substring(select_file.FileName.LastIndexOf("\\") + 1, select_file.FileName.Length - select_file.FileName.LastIndexOf("\\") - 5); // -5 = -4 - 3 letters of ending (you can cancel that if you want the ending) and 1 dot, and -1 - for the +1 we do in the first parameter to cancel the \ in the start. ---- modified ---- the other way is better (as they told you here). if you didn't see the examples so look here: file = System.IO.Path.GetFileName(select_file.FileName); // Will retrive the file's name with extension file = System.IO.Path.GetFileNameWithoutExtension(select_file.FileName); // Will retrive the file's name with out extension -- modified at 4:12 Friday 12th May, 2006

                S Offline
                S Offline
                Stefan Troschuetz
                wrote on last edited by
                #7

                Yes, and it is file = Path.GetFileName(select_file.FileName);


                "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                www.troschuetz.de

                1 Reply Last reply
                0
                • A Ashraj1982

                  There is no method called getfilename !! see I have this below OpenFileDialog select_file = new OpenFileDialog(); select_file.Title = "selected file"; select_file.Filter = "(*.txt) | *.txt"; if (select_file.ShowDialog() != DialogResult.Cancel) { file = select_file.FileName; // stores whole path with file name } but i want only selected file name in file not the path .

                  S Offline
                  S Offline
                  Stefan Troschuetz
                  wrote on last edited by
                  #8

                  Use the method I suggested: file = Path.GetFileName(select_file.FileName);


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de -- modified at 4:16 Friday 12th May, 2006

                  1 Reply Last reply
                  0
                  • A Ashraj1982

                    Hi, using openfiledialog to select a particular file and store the selected file name in a string it sores the filename with path so is there any way to store only the file name not the whole path? Thanks

                    N Offline
                    N Offline
                    nikeshkumar
                    wrote on last edited by
                    #9

                    Hi; this is simplest code u can use to get file name;first u have to import System.IO namespace. string path=openfiledialog1.PostedFile.FileName; string filename=Path.GetFileName(path); Response.Write(filename); it will work fine. with regards Nikesh Sahu nikesh

                    J 1 Reply Last reply
                    0
                    • N nikeshkumar

                      Hi; this is simplest code u can use to get file name;first u have to import System.IO namespace. string path=openfiledialog1.PostedFile.FileName; string filename=Path.GetFileName(path); Response.Write(filename); it will work fine. with regards Nikesh Sahu nikesh

                      J Offline
                      J Offline
                      J4amieC
                      wrote on last edited by
                      #10

                      :omg::wtf: That would be a good answer if the poster had asked about ASP.NET Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour

                      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