Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 21-Mar-19 4:19am
Comments
F-ES Sitecore 21-Mar-19 7:28am    
GetFiles returns multiple strings, so you'll need to go through each string in the filePaths array in a for loop, and do Process.Start on each one.
Member 12801143 21-Mar-19 7:49am    
can you show me how do I do that?

If you just want to launch the first file whose name contains the entered text:
VB.NET
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:
VB.NET
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
 
Share this answer
 
Comments
Member 12801143 21-Mar-19 11:29am    
Thanks Richard Deeming it works!!!
Use the FileOpen dialog box.

VB
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.
 
Share this answer
 
Comments
Member 12801143 21-Mar-19 10:07am    
Using dialog As New OpenFileDialog
If dialog.ShowDialog() <> DialogResult.OK Then Return
File.Copy(dialog.FileName, filePaths)
End Using

I do not understand what is wrong
#realJSOP 21-Mar-19 10:09am    
Well, I don't understand what's wrong either, because I'm not there to see what you're doing, and you're not telling us anything.

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