Results 1 to 9 of 9

Thread: [RESOLVED] The proper way to retrieve file names from a path?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Resolved [RESOLVED] The proper way to retrieve file names from a path?

    Hi, I'm using this to retrieve a file name from a path:

    Code:
    Dim fullPath As String = TextBox1.Text
    Dim fileName As String = IO.Path.GetFileName(fullPath)
    However, when I put it to use using something like this:

    Code:
    IO.File.Copy(TextBox1.Text, ListBox1.SelectedItem.ToString + "PSP\THEME\" + fileName)
    I get the 'NullReferenceException' error Can somebody help me with this?

    I'm sure it's just something I'm missing.

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

    Re: The proper way to retrieve file names from a path?

    Have you checked what the ListBox1.SelectedItem.ToString is?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

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

  3. #3
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: The proper way to retrieve file names from a path?

    I'm not sure I follow what you are asking, so I'm probably being useless here, but what the heck.
    Code:
    myString = Application.ProductName & ".exe"
    That will put your executables name in 'myString'.

  4. #4
    Addicted Member ThatSamiam's Avatar
    Join Date
    Apr 2007
    Posts
    128

    Re: The proper way to retrieve file names from a path?

    For your attempt run the code that results in the exception, what are the values in fullPath, fileName and ListBox1.SelectedItem.ToString?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: The proper way to retrieve file names from a path?

    I probably should have said that my listbox displays drive letters like 'C:\'. I just can't see what's wrong with the code.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: The proper way to retrieve file names from a path?

    is TextBox1.Text a valid path/filename?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: The proper way to retrieve file names from a path?

    It's a valid path.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: The proper way to retrieve file names from a path?

    It's most likely that there isn't an item selected in your listbox when you run that code... And since there is none selected, ListBox1.SelectedItem property returns Nothing on which you try to call the ToString method and so it results in a null reference exception.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: The proper way to retrieve file names from a path?

    Hmmm... I've just found something weird, on my development computer, it doesn't work but on another computer, it does :S

    Oh well, I guess it's resolved, thank you for your suggestions anyway

    stanav, I did have an item selected.

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