问题描述
您好,
我在VB.NET中有一个小应用程序,单击DatagriedView按钮时应搜索并打开Excel具有多个子目录的目录中的文件(2019年...... 2018 ......)。在这些子目录中有几个由数字命名的excell文件(5673455.xlsx ..... 11122232.xlsx ...)。此文件的名称应放在文本框中,单击按钮时应打开文件。
我怎样才能使它无需放入文本框中文件的全名并打开它?例如,完整文件名为23456_B_D.xlsx,仅放置文本框23456将打开该文件。如果文件的扩展名是xls而不是xlsx怎么办?
这是我到目前为止的代码。但是我收到一个错误:类型的'1维数组字符串'的值不能转换为'字符串'
我有什么试过:
Hello,
I have a small application in VB.NET that when clicking a button of DatagriedView should search and open an excel file within a directory with several subdirectories ordered by years (2019 .... 2018 ......). Within these subdirectories are several excell files named by numbers (5673455.xlsx ..... 11122232.xlsx ...). The name of this file should be placed in a textbox and when clicking the button should open the file.
How can I make it so that it is not necessary to put the full name of the file in the textbox and open it? For example the full file name is "23456_B_D.xlsx" and placing the textbox only "23456" opens the file. What if the file has the extension xls and not xlsx?
Here is the code I have so far. But I'm getting an error: "Value of type '1-dimensional array of String' can not be converted to 'String'
What I have tried:
Private Sub DataGridView_descricao_ColumnAdded(sender As System.Object, e As DataGridViewCellEventArgs) _
Handles DataGridView_descricao.CellContentClick
Dim senderGrid = DirectCast(sender, DataGridView)
If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso
e.RowIndex >= 0 Then
Dim filePaths As String() = Directory.GetFiles("C:\SALES", "*.xlsx", SearchOption.AllDirectories)
Dim fileName As String = TextBox_sa.Text + ".xlsx"
System.Diagnostics.Process.Start(Path.Combine(filePaths, fileName))
End If
End Sub
推荐答案
Dim allFiles As IEnumerable(Of String) = Directory.EnumerateFiles("C:\SALES", "*.xls?", SearchOption.AllDirectories)
Dim filePath As String = allFiles.FirstOrDefault(Function(f) f.IndexOf(TextBox_sa.Text, StringComparison.OrdinalIgnoreCase) <> -1)
If filePath Is Nothing Then
MessageBox.Show("File not found")
Else
System.Diagnostics.Process.Start(filePath)
End If
如果你想启动所有匹配的文件:
If you want to launch all matching files:
Dim allFiles As IEnumerable(Of String) = Directory.EnumerateFiles("C:\SALES", "*.xls?", SearchOption.AllDirectories)
Dim filePaths As IList(Of String) = allFiles.Where(Function(f) f.IndexOf(TextBox_sa.Text, StringComparison.OrdinalIgnoreCase) <> -1).ToList()
If filePaths.Count = 0 Then
MessageBox.Show("File not found")
Else
For Each filePath As String In filePaths
System.Diagnostics.Process.Start(filePath)
Next
End If
Sub SomeButton_Click(sender As Object, e As EventArgs) Handles SomeButton.Click
Using dialog As New OpenFileDialog
If dialog.ShowDialog() <> DialogResult.OK Then Return
File.Copy(dialog.FileName, newPath)
End Using
End Sub
我会留给你演示你的疯狂熟悉将代码段转换为适用于您自己的用例的内容。
I'll leave it up to you to demonstrate your mad skillz in molding that code snippet into something applicable to your own use case.
这篇关于从目录(包括所有子目录)获取文件并打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!