Results 1 to 4 of 4

Thread: Browse for File and get FileName [RESOLVED by MrPolite]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Browse for File and get FileName [RESOLVED by MrPolite]

    Hi there guys,

    I am in a desperate need to get to know the code for the following situations:
    I got 1 and 3 already, but I am struggling at 2 and 4.

    1. browse for folder and get folder path
    VB Code:
    1. MsgBox(FolderBrowserDialog1.SelectedPath)

    2. browse for folder and get folder name
    VB Code:
    1. MsgBox "Go to [url]www.vbforums.com[/url]"

    3. browse for file and get file path
    VB Code:
    1. MsgBox(OpenFileDialog1.FileName)

    4. browse for file and get file name
    VB Code:
    1. MsgBox "Go to [url]www.vbforums.com[/url]"

    Appreciate your time,
    Thank you,
    McoreD
    Last edited by ~*McoreD*~; Jan 15th, 2004 at 09:35 PM.

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: browse for file and get file name

    use System.IO.Path

    io.Path.GetDirectoryName gets the foldername
    IO.Path.GetFileNameWithoutExtension or IO.Path.GetFileName to get the file name


    for example, io.path.getdirectoryname( FolderBrowserDialog1.SelectedPath)
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Thumbs up Thank you!

    Hi MrPolite,

    Thank you for the prompt reply.

    With your code I was able to get the File Name perfectly.

    4. browse for file and get file name
    VB Code:
    1. txtFileName.Text = IO.Path.GetFileName(OpenFileDialog1.FileName)

    For the Directory name however, I had to change GetDirectoryName to GetFileName (very strange) in order to get the Directory Name.

    2. browse for folder and get folder name
    VB Code:
    1. txtRemotePath.Text = IO.Path.[b]GetFileName[/b](FolderBrowserDialog1.SelectedPath)

    Thank you very much for your help.
    McoreD

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Thank you!

    Originally posted by ~*McoreD*~
    VB Code:
    1. txtRemotePath.Text = IO.Path.[b]GetFileName<b>Hi MrPolite,
    2.  
    3. Thank you for the prompt reply. <img src="images/smilies/smile.gif" border="0" alt="" title="Smilie" class="inlineimg" />
    4.  
    5. With your code I was able to get the File Name perfectly.
    6.  
    7. 4. browse for file and get file name
    8. <div style="margin:20px; margin-top:5px">
    9.     <div class="smallfont" style="margin-bottom:2px">VB Code:</div>
    10.     <pre class="alt2" style="margin:0px; padding:px; border:1px inset; width:650px; max-height:372px;overflow:auto"><div dir="ltr" style="text-align:left;"><div class="vb" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">txtFileName.Text = IO.Path.GetFileName(OpenFileDialog1.FileName)</div></li></ol></div></div></pre>
    11. </div>
    12. For the Directory name however, I had to change GetDirectoryName to GetFileName (very strange) in order to get the Directory Name.
    13.  
    14. 2. browse for folder and get folder name
    15. </b>(FolderBrowserDialog1.SelectedPath)

    Thank you very much for your help.
    McoreD [/B]
    umm there is one big problem with all these functions and I hate that: some of them return the path ending with a "\" and some dont... I guess your path didnt end with a "\" and that's what GetDirectoryName didnt work.... I'll look into it


    edit: umm nm seems like my mistake. Just one thing to be aware of though. If your path ends with a "\", GetFileName will not return the directory name, but will return an empty string. But I don't think you need to worry about this, because the FileBrowserDialog will not return a path ending with a "\", except when you choose a root path (ie if you choose drive C it will return "C:\" but if you choose the windows directory it would return something like "C:\windows")
    Last edited by MrPolite; Jan 15th, 2004 at 09:43 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width