Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Is there another way to list files? As it wont let me remove from the listbox using datasource

 

   Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
       If lstDisplay.SelectedIndex <> -1 Then
           lstDisplay.Items.RemoveAt(lstDisplay.SelectedIndex)
       End If
   End Sub

   Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
       'Search for torrent files
       Dim torrents() As String = System.IO.Directory.GetFiles("C:\Documents and Settings\Sam\Application Data\uTorrent", "*.torrent")
       'Display found files
       lstDisplay.DataSource = torrents
   End Sub

  • Administrators
Posted

Rather than an array could you not bind to a list of strings and simply remove the item from the underlying list?

Something like

   Dim torrentList As List(Of String)

   Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       If lstDisplay.SelectedIndex <> -1 Then
           torrentList.Remove((lstDisplay.SelectedValue))
           lstDisplay.DataSource = Nothing
           lstDisplay.DataSource = torrentList
       End If
   End Sub

   Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       'Search for torrent files
       Dim torrents() As String = System.IO.Directory.GetFiles("C:\Documents and Settings\Sam\Application Data\uTorrent", "*.torrent")
       torrentList = torrents.ToList()

       'Display found files
       lstDisplay.DataSource = torrentList
   End Sub

 

Rather than the iffy bit of code setting the datasource to nothing and back you could also use a DataSource control...

 

As an aside you are better using code like

  Dim torrents() As String = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "uTorrent"), "*.torrent")
   

instead of hard coding paths.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Thank you for the reply, for learning purposes can you explain how this code works, seems a lot tidier

Dim torrents() As String = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "uTorrent"), "*.torrent")

 

But vb.net does highlight Directory and path saying it's not declared. I am very new to coding so sorry if i am being stupid.

 

I will test your other code now. But to be honest in the future i would like to be able to search all the c:\ drive or the location stated in txtDir rather then the specific location

 

 

Thanks again

  • Administrators
Posted

You will need to add an

Imports System.Io

to the top of the source file to get it to compile.

 

the Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData bit will return the path for the current users application data - this can vary by OS or be configured differently to the normal OS' default so you are better avoiding hard coded paths.

 

Path.Combine simply builds a valid path from two strings - I tend to use it because it handles leading / trailing or omitted path separators (/ or \) plus the fact the code expresses it's intention more clearly than just concatenating strings.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...