Hello,
I need help with making a code that opens a random file in a directory...
The files will be called:
1.txt
2.txt
etc...
Could someone please reply?
You will need to use directory info.
Here is an example.
Private Sub GetRandFile(sender As Object, e As EventArgs)
Try
Dim tmpdir As String = My.Computer.FileSystem.SpecialDirectories.Desktop
Dim tmpar() As String
If Directory.Exists(tmpdir) = True Then
Dim di As New IO.DirectoryInfo(tmpdir)
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
For Each dra In diar1
tmpar = dra.FullName.Split(".")
If tmpar(1) = ".txt" Then
Process.Start(dra.FullName)
End If
Next
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
This can just be assigned as an event handler for a button or any control of your chosing.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.