将目录列表复制到文本文件 [英] Copy directory listing to text file

查看:70
本文介绍了将目录列表复制到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将目录(&子目录)列表复制到vb.net中的文本

文件。


我正在寻找相当于DOS命令dir [SourceDirectory] ​​/ s>

TargetFile.txt。

谢谢!

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!

推荐答案

*" F.迈克尔米勒 < FM *** @ netzero.net> scripsit:
* "F. Michael Miller" <fm***@netzero.net> scripsit:
我想将目录(&子目录)的列表复制到vb.net中的文本
文件。

我'我正在寻找等同于DOS命令dir [SourceDirectory] ​​/ s>
TargetFile.txt。
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.




枚举文件夹中的文件及其子文件夹:


< http://www.mvps.org/dotnet/dotnet/samples/filesystem/downloads/RecursiveFileScan.zip>


为了编写文件,你可以使用''System.IO.StreamWriter''。


-

Herfried K. Wagner [MVP]

< http://www.mvps.org/dotnet>



Enumerating files in a folder and its subfolders:

<http://www.mvps.org/dotnet/dotnet/samples/filesystem/downloads/RecursiveFileScan.zip>

For writing the file, you can use ''System.IO.StreamWriter''.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


文章< 40 ********* **************@newsreader.visi.com>,F。Michael Miller写道:
In article <40***********************@newsreader.visi.com>, F. Michael Miller wrote:
我想复制一个目录列表(& ;子目录)到vb.net中的文本
文件。

我正在寻找等同于DOS命令dir [SourceDirectory] / s>
TargetFile.txt。

谢谢!
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!




怎么回事:


选项显式开启

选项严格开启


进口System.Diagnostics

进口System.IO


公共类Form1

继承System.Windows.Forms.Form


#Region" Windows窗体设计器生成的代码


Public Sub New()

MyBase.New()


' 'Windows窗体设计器需要此调用。

InitializeComponent()

''在InitializeComponent()调用后添加任何初始化
< br $>
结束子


''表格覆盖处理以清理组件列表。

受保护的重载覆盖子处理(ByVal处理为

布尔值)

如果处理那么

如果不是(组件什么都没有)那么

components.Dispose()

结束如果

结束如果

MyBase.Dispose(处置)

End Sub


''Windows窗体设计器要求

私有组件As System.ComponentModel.IContainer


''注意:以下过程是Windows窗体要求

设计器

''可以使用Windows窗体设计器修改它。

''不要使用代码编辑器修改它。

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.ou tputFile.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 =新系统。 Drawing.Point(4,32)

Me.Label2.Name =" Label2"

Me.Label2.TabIndex = 3

Me .Label2.Text ="输出文件名"

Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft

''

''去

''

Me.go.Location = New System.Drawing.Point(109,64)

Me.go.姓名=" 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 Region

Private Sub go_Click(ByVal sender As System.Object,ByVal e作为

System.EventArgs)处理go.Click

Dim命令As New Process


go.Enabled = False


使用command.StartInfo

.FileName =

System.Environment.GetEnvironmentVariable(" COMSPEC")

.Arguments =" / c dir / s" &安培; Me.sourceDirectory.Text

.CreateNoWindow = True

.UseShellExecute = False

.RedirectStandardOutput = True

结束使用


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()

循环


output.Close()

command.Dispose()

go.Enabled = True

End Sub


结束班级


显然,那里在这里没有错误陷阱等等 - 所以你想要在实际场景中添加




-

Tom Shelton [MVP]

操作系统名称:Microsoft Windows XP Professional

操作系统版本:5.1.2600 Service Pack 1 Build 2600
系统运行时间:0天,19 Ho urs,40分钟,6秒



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


很好的例子。

问候 - OHM

Tom Shelton写道:
Good example.
regards - OHM
Tom Shelton wrote:
文章< 40 *********************** @ newsreader.visi.com>,F。
Michael Miller写道:
In article <40***********************@newsreader.visi.com>, F.
Michael Miller wrote:
我想复制一个目录列表(&子目录)到vb.net中的
文本文件。

我正在寻找等同于DOS命令的目录
[SourceDirectory] ​​/ s> TargetFile.txt。

谢谢!
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!



怎么回事:

Option Explicit On
Option Strict On

进口System.Diagnostics
进口System.IO

Public Class Form1
继承System.Windows.Forms.Form
#Region" Windows窗体设计器生成的代码

Public Sub New()
MyBase.New()

''Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化

End Sub

''表格覆盖处理清理组件列表。
受保护的过载覆盖子处理(ByVal处理为布尔)
如果处理则
如果不是(组件什么都没有)那么
components.Dispose ()
结束如果
结束如果
MyBase.Dispose(处理)
结束Sub

''Windows窗体设计师要求
私有组件As System.ComponentModel.IContainer

''注意:Windows窗体
设计器需要以下过程
''可以使用Windows修改它表单设计器。
''不要使用代码编辑器修改它。
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 =新的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.S ize = 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 ="输出文件名"
Me.Label2.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft''
''去
''
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 Region
私人子go_Click(ByVal发送者As System.Object,ByVal e As
System.EventArgs)处理go.Click
昏暗命令为新进程

go.Enabled = False

使用command.StartInfo
.FileName =
System.Environment.GetEnvironmentVar iable(COMSPEC)
.Arguments =" / c dir / s" &安培; Me.sourceDirectory.Text
.CreateNoWindow = True
。UseShellExecute = False
。RedirectStandardOutput = True
结束

command.Start() File.CreateText(Me.outputFile.Text)Dim line As String =
command.StandardOutput.ReadLine()Do Until line is Nothing
output.WriteLine(line)
line = command.StandardOutput.ReadLine()
循环

output.Close()
command.Dispose()
go.Enabled = True
End Sub

结束课

显然,这里没有错误捕获等等 - 所以你会想要
在真实场景中添加它。



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.




-

最诚挚的问候 - OHM

O_H_M {at} BTInternet {dot} com



--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com


这篇关于将目录列表复制到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆