Results 1 to 15 of 15

Thread: [RESOLVED] [2005] get the filename only from the open file dialog

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Resolved [RESOLVED] [2005] get the filename only from the open file dialog

    Hi guys,
    I have 2 questions

    1. I wanna save a picture but only the filename only not the whole path.
    how do I do that using open file dialog.
    2. Let say I did save that file, how do I copy that file from the origin path to
    specific path

    Thanks guys.
    effort effort effort and still effort

  2. #2
    Lively Member dsuraj's Avatar
    Join Date
    Nov 2007
    Location
    Pune
    Posts
    70

    Re: [2005] get the filename only from the open file dialog

    use openfiledialogue.filename property

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Re: [2005] get the filename only from the open file dialog

    but it stores the whole path. like "C:/blabla/hkbkabek/a.jpg"
    I just want the a.jpg. thanks for helping anyway.
    effort effort effort and still effort

  4. #4
    Lively Member dsuraj's Avatar
    Join Date
    Nov 2007
    Location
    Pune
    Posts
    70

    Re: [2005] get the filename only from the open file dialog

    Use substring function

  5. #5
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] get the filename only from the open file dialog

    Actually, no

    do
    vb.net Code:
    1. TempFile As New System.IO.FileInfo(ofd.FileName)
    TempFile.Name (the name includes the extension)

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] get the filename only from the open file dialog

    another alternative would be:

    Dim FileNameOnly As String = System.IO.Path.GetFileName(ofd.FileName)

    Both Path.GetFileName and FileInfo work really well and is self documenting
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Re: [2005] get the filename only from the open file dialog

    GREAT!! I can't wait to try those codes. Anyway thank you for helping guys.

    oh by the way is there anyone that could me for question # 2? I did some research last night, I tried it but it's not working. I will post my code later today because I am still at school.
    effort effort effort and still effort

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] get the filename only from the open file dialog

    System.IO.File.Copy

    Look it up on MSDN for a code example
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Re: [2005] get the filename only from the open file dialog

    yayyy the fileNameOnly works. I have one quick question.
    in my form load I use the following code to load the image

    Code:
     currentRow = Me.BongoComicsDataSet.Characters.Rows(index)
                lblName.Text = currentRow("name")
                Pict1.Image = Image.FromFile("Characters\" & CStr(currentRow("image")))
    it works just fine BUT everytime I add new picture I want automatically reload again the first picture, So what I did is using the same code to load it but it says FileNotFound. is the path changing everytime I add new picture from different folder?

    it seems the program didn't know where is
    Code:
    Pict1.Image = Image.FromFile("Characters\" &CStr(currentRow("image")))
    because when I add picture I add a picture form "My documents" or "desktop"
    effort effort effort and still effort

  10. #10
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] get the filename only from the open file dialog

    The picture is created by you yes? and 'currentRow("image")' contains the path to the image?, and how are you creating a new picture?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Re: [2005] get the filename only from the open file dialog

    This is how I add the new picture
    Code:
    fileNameOnly = System.IO.Path.GetFileName(OFD.FileName)
                CharactersTableAdapter.Insert(txtName.Text, fileNameOnly, txtAKA.Text)
                Me.Validate()
                Me.CharactersTableAdapter.Update(Me.BongoComicsDataSet.Characters)
                Me.CharactersTableAdapter.Fill(Me.BongoComicsDataSet.Characters)
    Yes it contains the path BUT not the whole path. What I mean not the whole path is, it contains only "Characters/filename.jpg" not "C:\Documents and Settings\erika\My Documents\Visual Studio 2005\Projects\BongoComics\BongoComics\bin\Debug\Characters\filename.jpg"

    it works fine if I put the whole code, but this codes won't work in someone else computer because the path might be different.
    the same thing when I copy the picture from the sourcePath to destinationPath. It will work if I put the whole path.
    this is my code to copy the file
    Code:
    My.Computer.FileSystem.CopyFile(filename, "C:\Documents and Settings\erika\My Documents\Visual Studio 2005\Projects\BongoComics\BongoComics\bin\Debug\Characters\" & fileNameOnly)
    effort effort effort and still effort

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] get the filename only from the open file dialog

    if you are trying to indicate the current directory the application is running in (or a subdirectory of that) you should use Application.StartupPath

    When you run your app, the actual EXE right now is located at

    C:\Documents and Settings\erika\My Documents\Visual Studio 2005\Projects\BongoComics\BongoComics\bin\Debug\

    However that is obviously not where it will always exist.

    So the variable Application.StartupPath will be equal to

    C:\Documents and Settings\erika\My Documents\Visual Studio 2005\Projects\BongoComics\BongoComics\bin\Debug\

    if your exe was located in

    c:\program files\my company\my app\

    then that would be the value of application.startuppath (minus the final \)

    So what I am getting at is, try

    Code:
    My.Computer.FileSystem.CopyFile(filename, Application.StartupPath & "\characters\" & filenameonly)

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    228

    Re: [2005] get the filename only from the open file dialog

    AWESOME kleinma!!! thanks a lot. Thank you all guys. I really appreciate it.
    effort effort effort and still effort

  14. #14
    New Member
    Join Date
    Nov 2015
    Posts
    1

    Re: [2005] get the filename only from the open file dialog

    Quote Originally Posted by ayahnabunda View Post
    but it stores the whole path. like "C:/blabla/hkbkabek/a.jpg"
    I just want the a.jpg. thanks for helping anyway.
    use this code:

    dim filnam as string = OpenFileDialog.SafeFileName

  15. #15
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] get the filename only from the open file dialog

    Quote Originally Posted by zgjir View Post
    use this code:

    dim filnam as string = OpenFileDialog.SafeFileName
    The OFD didn't have that back in 2007, this is a super old thread & needn't be brought up again.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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