Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am having a little issue and on achieving a certain function in my project.

I currently have a form with a ListBox and a RichTextBox. I was wondering how I could make the ListBoxs list a certain folder directory on my computer that contains RTF files and then to load them into the RichTextBox.

Any ideas.

I know I can uses the RTF.LoadFile() but how do I make the ListBox file the filename/path.
Posted
Updated 6-Feb-11 9:12am
v2

Thanks to SA who got me on the right path with the system.IO.getfiles.

For those who need the following code it loads a specified directory contents based on file pattern into a listbox.

<br />
Private Sub btnList_Click(ByVal sender As System.Object, _<br />
    ByVal e As System.EventArgs) Handles btnList.Click<br />
    ' Get the pattern without stuff in parens.<br />
    Dim pattern As String = cboPattern.Text<br />
    If pattern.IndexOf("(") >= 0 Then<br />
        pattern = pattern.Substring(0, pattern.IndexOf("("))<br />
    End If<br />
<br />
    ' Get the files.<br />
    lstFiles.Items.Clear()<br />
    Dim dir_info As New DirectoryInfo(txtDir.Text)<br />
    Dim file_infos() As FileInfo<br />
    file_infos = dir_info.GetFiles(pattern)<br />
    For Each file_info As FileInfo In file_infos<br />
        lstFiles.Items.Add(file_info.Name)<br />
    Next file_info<br />
End Sub



References:
Directory.Get.Files search pattern problem[^]

http://www.vb-helper.com/howto_net_list_directory.html[^]
 
Share this answer
 
Use System.IO.Directory.GetFiles.

You need to be extra careful, see the discussion here: Directory.Get.Files search pattern problem[^].

The ultimately correct answer was given my Abhishek Sur; see also my answers and discussion. In your case it should be:

var files =
   System.IO.Directory.GetFiles(
      directory,
      "*.rtf").Where(
         item =>
         item.Where(item => item.ToLower().EndsWith(".rtf"));


If you simply use GetFiles without Where, it might also return all the files with names like *.rtfa, *.rtfab, that is, *.rtf?, *.rtf??, and so on.

—SA
 
Share this answer
 
v5
Comments
Sandeep Mewara 6-Feb-11 13:14pm    
Simple and direct! 5.
Sergey Alexandrovich Kryukov 6-Feb-11 13:27pm    
Thank you.
Fixed, made case-insensitive.
--SA
Sandeep Mewara 6-Feb-11 13:29pm    
Now, 5++ :)
SIFNOk 6-Feb-11 13:31pm    
how would this be implemented in vb.net, unfortantly i only understand vb.net.
Im sorry, i should have stated what i was using at the start.

is there a simplier way like read the contents of another textfile with the path locations.
Sergey Alexandrovich Kryukov 6-Feb-11 13:37pm    
Sorry, you'll have to translate, should be easy.
I don't have any VB/VB.NET and nothing can make me to install it. :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900