I'd like to copy the listing of a directory (& sub directories) to a text
file in vb.net.
I'm looking for teh equivilant of the DOS command dir [SourceDirectory] /s >
TargetFile.txt.
Thanks!
How 'bout this:
Option Explicit On
Option Strict On
Imports System.Diagnostics
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents sourceDirectory As System.Windows.Forms.TextBox
Friend WithEvents outputFile As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents go As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.sourceDirectory = New System.Windows.Forms.TextBox
Me.outputFile = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.go = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'sourceDirectory
'
Me.sourceDirectory.Location = New System.Drawing.Point(108, 4)
Me.sourceDirectory.Name = "sourceDirectory"
Me.sourceDirectory.Size = New System.Drawing.Size(180, 20)
Me.sourceDirectory.TabIndex = 0
Me.sourceDirectory.Text = ""
'
'outputFile
'
Me.outputFile.Location = New System.Drawing.Point(108, 32)
Me.outputFile.Name = "outputFile"
Me.outputFile.Size = New System.Drawing.Size(180, 20)
Me.outputFile.TabIndex = 1
Me.outputFile.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(4, 4)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 2
Me.Label1.Text = "Source Directory"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(4, 32)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 3
Me.Label2.Text = "Output File Name"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'go
'
Me.go.Location = New System.Drawing.Point(109, 64)
Me.go.Name = "go"
Me.go.TabIndex = 4
Me.go.Text = "&Go"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 93)
Me.Controls.Add(Me.go)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.outputFile)
Me.Controls.Add(Me.sourceDirectory)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub go_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles go.Click
Dim command As New Process
go.Enabled = False
With command.StartInfo
.FileName =
System.Environment.GetEnvironmentVariable("COMSPEC")
.Arguments = "/c dir /s " & Me.sourceDirectory.Text
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With
command.Start()
Dim output As StreamWriter = File.CreateText(Me.outputFile.Text)
Dim line As String = command.StandardOutput.ReadLine()
Do Until line Is Nothing
output.WriteLine(line)
line = command.StandardOutput.ReadLine()
Loop
output.Close()
command.Dispose()
go.Enabled = True
End Sub
End Class
Obviously, there is no error trapping, etc. in this - so you would want
to add that in a real world scenario.
--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 19 Hours, 40 Minutes, 6 Seconds