列表框-加载目录文件名 [英] Listbox - load directory file names

查看:137
本文介绍了列表框-加载目录文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到了一个小问题,正在实现我的项目中的某些功能.

我目前有一个带有ListBox和RichTextBox的表单.我想知道如何使ListBoxs列出计算机上包含RTF文件的某个文件夹目录,然后将它们加载到RichTextBox中.

有什么想法.

我知道我可以使用RTF.LoadFile(),但是如何使ListBox文件成为文件名/路径.

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.

推荐答案

使用System.IO.Directory.GetFiles.

您需要格外小心,请参阅此处的讨论: Directory.Get.文件搜索模式问题 [ ^ ].

最终正确的答案是我的Abhishek Sur.另请参阅我的答案和讨论.您的情况应该是:

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"));



如果仅使用GetFiles而不使用Where,它可能还会返回所有带有* .rtfa,*.rtfab,即* .rtf?,*.rtf ??等名称的文件. />

—SA



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


感谢SA,SA使我与system.IO.getfiles一起走上了正确的道路.

对于需要以下代码的用户,它将根据文件模式将指定的目录内容加载到列表框中.

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




参考文献:
Directory.Get.Files搜索模式问题 [ ^ ]

http://www.vb-helper.com/howto_net_list_directory.html [




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

http://www.vb-helper.com/howto_net_list_directory.html[^]


这篇关于列表框-加载目录文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆