In article <40************ ***********@new sreader.visi.co m>, F. Michael Miller wrote:
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.Diagnost ics
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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.sourceDirect ory = 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.SuspendLayou t()
'
'sourceDirector y
'
Me.sourceDirect ory.Location = New System.Drawing. Point(108, 4)
Me.sourceDirect ory.Name = "sourceDirector y"
Me.sourceDirect ory.Size = New System.Drawing. Size(180, 20)
Me.sourceDirect ory.TabIndex = 0
Me.sourceDirect ory.Text = ""
'
'outputFile
'
Me.outputFile.L ocation = New System.Drawing. Point(108, 32)
Me.outputFile.N ame = "outputFile "
Me.outputFile.S ize = New System.Drawing. Size(180, 20)
Me.outputFile.T abIndex = 1
Me.outputFile.T ext = ""
'
'Label1
'
Me.Label1.Locat ion = New System.Drawing. Point(4, 4)
Me.Label1.Name = "Label1"
Me.Label1.TabIn dex = 2
Me.Label1.Text = "Source Directory"
Me.Label1.TextA lign = System.Drawing. ContentAlignmen t.MiddleLeft
'
'Label2
'
Me.Label2.Locat ion = New System.Drawing. Point(4, 32)
Me.Label2.Name = "Label2"
Me.Label2.TabIn dex = 3
Me.Label2.Text = "Output File Name"
Me.Label2.TextA lign = System.Drawing. ContentAlignmen t.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.AutoScaleBas eSize = 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.sourceDirec tory)
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.EventArg s) Handles go.Click
Dim command As New Process
go.Enabled = False
With command.StartIn fo
.FileName =
System.Environm ent.GetEnvironm entVariable("CO MSPEC")
.Arguments = "/c dir /s " & Me.sourceDirect ory.Text
.CreateNoWindow = True
.UseShellExecut e = False
.RedirectStanda rdOutput = True
End With
command.Start()
Dim output As StreamWriter = File.CreateText (Me.outputFile. Text)
Dim line As String = command.Standar dOutput.ReadLin e()
Do Until line Is Nothing
output.WriteLin e(line)
line = command.Standar dOutput.ReadLin e()
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