单独打开.doc和.rtf文件 [英] opening .doc and .rtf file alone

查看:87
本文介绍了单独打开.doc和.rtf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的编码中,显示了所选文件夹中的所有文件,但是我只想在ListBox中显示.doc和.rtf文件,请指导我编码部分,请紧迫. ................

In the below coding all the files in the selected folder are displayed, but I only want to display the .doc and .rtf files in the ListBox, kindly guide me the coding part , its urgent please.................

Imports System.IO
Imports System.Text
Imports System.Data
Imports System.Windows.Forms

Public Class Form1
    Dim flb As New System.Windows.Forms.FolderBrowserDialog
    Dim fod As OpenFileDialog = New OpenFileDialog()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        flb.RootFolder = Environment.SpecialFolder.MyComputer
        flb.ShowNewFolderButton = False
        Dim result As DialogResult = flb.ShowDialog
        
        If (result = DialogResult.OK) Then
            ListBox1.Items.Clear()
            TextBox1.Text = flb.SelectedPath
            For Each filename In Directory.GetFiles(flb.SelectedPath)
             ListBox1.Items.Add(filename)
            Next
        End If

    End Sub
End Class

推荐答案

修改
For Each filename In Directory.GetFiles(flb.SelectedPath)


成为


to be

For Each filename In Directory.GetFiles(flb.SelectedPath,"*.doc").Union(Directory.GetFiles(flb.SelectedPath, "*.rtf"))


您可以使用LinQ过滤出.doc或.rtf的文件名,如下所示:-

You can filtre out the filenames that are .doc or .rtf with LinQ like this:-

Using flb As New FolderBrowserDialog
           If flb.ShowDialog() = Windows.Forms.DialogResult.OK Then
               ListBox1.Items.Clear()
               TextBox1.Text = flb.SelectedPath
               Dim fileNames As String() = Directory.GetFiles(flb.SelectedPath)
               Dim selectedFiles As String() = From fileName In fileNames Where fileName.EndsWith(".rtf") Or fileName.EndsWith(".doc") Select fileName
               For Each filename In selectedFiles
                   ListBox1.Items.Add(filename)
               Next
           End If
       End Using



希望这有助于



Hope this helps


使用过滤器仅获取.DOC和.RTF文件

use the filter to get only .DOC and .RTF files

dim files as string() = System.IO.Directory.GetFiles(path, "*.doc;*.rtf", SearchOption.AllDirectories)


这篇关于单独打开.doc和.rtf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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