0

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?

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Begginnerdev
0

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.

Edited by Begginnerdev: edit

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.