Thread: Getting just the filename from a path

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    85

    Getting just the filename from a path

    i need to extract just the filename from a full path when using the saveFileDialog.

    i have noticed in the openFileDialog they give the "SafeFilename" to do that job but it is not there in the saveFileDialog options.

    so i guess i need to use either basic string handling or maybe stringbuilder here ?

    however my string handling experience in c# is not good i would like to know how to achieve this.

    if my path is

    c:\users\me\test\filename.txt

    how can i strip just the "filename.txt" part to save in another string ( or even just chop the exsiting string as it is not needed after this point)

    thanks

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Path.GetFilename(...)

    or if u wanna do it string search way:
    Code:
    string[] split = path.Split('\\');
    string filename = split[split.Length - 1];
    and of course check for errors in the input string.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    thanks Bling - sorted

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Perhaps something like

    Code:
                OpenFileDialog op = new OpenFileDialog();
                op.ShowDialog();
    
                MessageBox.Show(op.FileName.Substring(op.FileName.LastIndexOf("\\") + 1));

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Don't do it by hand when there's excellent classes in System.IO.Path for you!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Hehe Cool ^ Learn something new about the framework every day.

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    It never seizes to amaze me how lazy people so persistently do things the hard way, even though it means that it goes against their lazy nature.

    Google, people. GOOGLE. First result. Or second for more concise results.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    shift+tab, grab the mouse, move 45deg up-right, type, press <enter>, move mouse 45dg up donwn and click on link. Read link. Copy paste.... too much work.
    Moral: lazy people are doomed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Using string operations to save part of a file path
    By JackR in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2007, 03:48 PM
  3. user defined path for FILENAME
    By drdigimon in forum C Programming
    Replies: 5
    Last Post: 10-12-2003, 06:04 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. getting a file's path and filename
    By face_master in forum Windows Programming
    Replies: 4
    Last Post: 02-05-2002, 01:44 PM