Michel-IT Michel-IT - 3 years ago 84
Vb.net Question

Vb.net receives the directory list in the specific directory and saves it in a file.txt

I need to create a list of existing directory names in a specific folder



Dim WorksList As String = Application.StartupPath + "\db\works.txt"
SlcTextBox_BaseWorksDirectory = "c:\ListDirectory"

If My.Computer.FileSystem.DirectoryExists(SlcTextBox_BaseWorksDirectory.Text) Then
Saves all existing directory names SlcTextBox_BaseWorksDirectory.text (c:\ListDirectory) in a WorksList (works.txt)
End If





File works.txt

ADirectory1

BDirectory2

CDirectory3

DDirectory4

EDirectory5

FDirectory6

GDirectory7

Answer Source

To print the existing directories of a specific directory to a file, simply retrieve the files using GetDirectories and print using AppendAllLines. You can replace My.Computer.FileSystem.SpecialDirectories.MyDocuments with the path to the directory you want to list.

    Dim directories = My.Computer.FileSystem.GetDirectories(My.Computer.FileSystem.SpecialDirectories.MyDocuments)

    IO.File.AppendAllLines(WorksList, directories.Select(Function(f) f.Substring(f.LastIndexOf("\") + 1)))

This assumes a WorksList points to a valid existing text file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download