.Net print directory list file names only to file

Using vb.Net
I would like to output to a text file a list of the file names in a directory.
Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Simple example:

Dim dir As New IO.DirectoryInfo(Directory.GetCurrentDirectory())

Using sw As StreamWriter = New StreamWriter(Path.Combine(dir.FullName, "files.txt"))

    Dim files As IO.FileInfo() = dir.GetFiles()

    For Each file In files
    Next file

End Using

Open in new window

Fernando SotoRetiredCommented:
Hi lrbrister;

Here is sample code to do what you want.
Private Sub SaveFilenamesToFile(ByVal sourceDir As String, ByVal destDir As String, ByVal saveFileAs As String)
    '' Make sure that the Source Directory does exist
    If Not Directory.Exists(sourceDir) Then
        MessageBox.Show("Source Directory " + sourceDir + " Doest Not Exist")
    End If

    '' Make sure that the Destination Directory does exist
    If Not Directory.Exists(destDir) Then
        MessageBox.Show("Destination Directory " + destDir + " Doest Not Exist")
    End If

    '' Get the DirectoryInfo for the source directory
    Dim sDir = FileIO.FileSystem.GetDirectoryInfo(sourceDir)
    '' Get an array of FileInfo in the directory
    Dim names = sDir.GetFiles("*")

    '' Create / open the destination file
    Dim fs = File.Open(destDir & "\" & saveFileAs, FileMode.OpenOrCreate, FileAccess.Write)
    '' Get a stream writer
    Dim sw As New StreamWriter(fs)
    '' Writes the directiory name as the first line in the file
    sw.WriteLine("Directory " + destDir)
    sw.WriteLine(" ")
    '' Write all the file names to the file stream
    For Each name As FileInfo In names
    '' Clean up
End Sub

Open in new window

You can call the above function as follows
SaveFilenamesToFile("C:\Working Directory", "C:\Working Directory\Test", "Filename.txt")

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Larry Bristersr. DeveloperAuthor Commented:
Thanks guys... sorry for the late get back
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.