问题描述
大家好,
我遇到了一个小问题,正在实现我的项目中的某些功能.
我目前有一个带有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.
推荐答案
var files =
System.IO.Directory.GetFiles(
directory,
"*.rtf").Where(
item =>
item.Where(item => item.ToLower().EndsWith(".rtf"));
如果仅使用GetFiles
而不使用Where
,它可能还会返回所有带有* .rtfa,*.rtfab,即* .rtf?,*.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.
<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[^]
这篇关于列表框-加载目录文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!