使用FSO,TextStream等所需的帮助---请求代码审查和建议 [英] Help needed in using FSO's, TextStreams, etc. --- Code Review and Advice requested

查看:61
本文介绍了使用FSO,TextStream等所需的帮助---请求代码审查和建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想了解您对以下代码的评论。 sub完全按照我的要求去做,但我觉得它并不像所有人那样坚固。使用一些VB6代码,.Net2003代码和.Net2005代码,好像我是
。我正在vb.net 2005中开发。


这个测试子只是读取输入文本文件,将记录写入另一个文本文件,消除了具有99的记录'在它们中(类似于

a CSV文件)。


我的一些担忧是:


1,''writeline''在键入时不会转换为大写/小写 - 我错过了参考吗?

2.我是否不必要地将太多的对象设置为''= Nothing''?

3.重命名声明好像是VB6 - 是不是有文本流重命名?


我想这样做vb。网络方式尽可能多,但似乎在搜索网络时我最终混合了VB6,Net2003和Net2005

代码。


感谢您的帮助,


Hexman

Private Sub RemoveRecs()

''********* **************************************** ********** ******************

''这个例程读取.pgh文件并删除其中包含'99''

''的所有记录。创建一个输出文件(没有99'),删除原始文件

'并将新文件重命名为原始文件。

''* ************************************************ ** ************************** *

Const ForReading = 1

Const ForWriting = 2

Dim FSO''文件脚本对象

Dim FSO1''文件脚本对象

Dim TSI''输入文本流

Dim TSO''输出文本流

Dim strRecord As String

Dim InFileName As String

Dim OutFileName As String

Dim AnyChanges As Boolean


AnyChanges = False

InFileName =" C:\ jjj.txt"

OutFileName =" c:\kkk.txt"

FSO = CreateObject(" Scripting.FileSystemObje ct")

FSO1 = CreateObject(" Scripting.FileSystemObject")

TSI = FSO.OpenTextFile(InFileName,ForReading,True)

TSO = FSO1.opentextfile(OutFileName,ForWriting,True)

Do TSI.AtEndOfStream< True

strRecord = TSI.ReadLine

如果InStr(1,strRecord," 99")= 0然后

TSO.writeline(strRecord)

否则

AnyChanges = True

结束如果

循环

TSI.Close()

TSO.close()

如果AnyChanges那么

TSI = FSO.GetFile(InFileName)

TSI.Delete()

重命名(OutFileName,InFileName)

Else

TSO = FSO.GetFile(OutFileName)

TSO.Delete()

结束如果

FSO =没什么''清理 - 摧毁物品

FSO1 =没什么

TSI =没什么

TSO =没什么

End Sub

Hello All,

I''d like your comments on the code below. The sub does exactly what I want it to do but I don''t feel that it is solid as all. It seems like I''m
using some VB6 code, .Net2003 code, and .Net2005 code. I''m developing in vb.net 2005.

This test sub just reads an input text file, writing out records to another text file, eliminating records that have a ''99'' in them (it is similar to
a CSV file).

Some of my concerns are:

1, ''writeline'' does not convert to upper/lower case when keyed in - am I missing a reference?
2. Am I unnecessarily setting too many objects to ''= Nothing''?
3. The ''Rename'' statement seems like VB6 - isn''t there a textstream rename?

I want to do this the vb.net way as much as possible, but it seems that while searching the net that I wind up with a mix of VB6, Net2003 and Net2005
code.

I appreciate your help,

Hexman
Private Sub RemoveRecs()
''************************************************* ****************************
'' This routine reads the .pgh file and eliminates any records that have a ''99''
'' in them. An output file is created (without the 99''s), the original file is
'' deleted and the new file is renamed to the original.
''************************************************* ****************************
Const ForReading = 1
Const ForWriting = 2
Dim FSO '' A File Scripting object
Dim FSO1 '' A File Scripting object
Dim TSI '' Input Text Stream
Dim TSO '' Output Text Stream
Dim strRecord As String
Dim InFileName As String
Dim OutFileName As String
Dim AnyChanges As Boolean

AnyChanges = False
InFileName = "C:\jjj.txt"
OutFileName = "c:\kkk.txt"
FSO = CreateObject("Scripting.FileSystemObject")
FSO1 = CreateObject("Scripting.FileSystemObject")
TSI = FSO.OpenTextFile(InFileName, ForReading, True)
TSO = FSO1.opentextfile(OutFileName, ForWriting, True)
Do While TSI.AtEndOfStream <True
strRecord = TSI.ReadLine
If InStr(1, strRecord, "99") = 0 Then
TSO.writeline(strRecord)
Else
AnyChanges = True
End If
Loop
TSI.Close()
TSO.close()
If AnyChanges Then
TSI = FSO.GetFile(InFileName)
TSI.Delete()
Rename(OutFileName, InFileName)
Else
TSO = FSO.GetFile(OutFileName)
TSO.Delete()
End If
FSO = Nothing ''Clean up - destroy objects
FSO1 = Nothing
TSI = Nothing
TSO = Nothing
End Sub

推荐答案



" Hexman" <他**** @ binary.com写了留言

新闻:f8 ************************** ****** @ 4ax.com ...


"Hexman" <He****@binary.comwrote in message
news:f8********************************@4ax.com...

大家好,


我想要你的对以下代码的评论。 sub确实完成了我想要它做的事情,但我觉得它并不像所有人那样坚固。使用一些VB6代码,.Net2003代码和.Net2005代码,好像我是
。我正在开发

vb.net 2005.


这个测试子只读取输入文本文件,写出记录到

另一个文本文件,删除其中包含99的记录(

类似于

a CSV文件)。


我的一些顾虑是:


1,''writeline''在键入时不会转换为大写/小写 - 我是

缺少参考?

2.我是否不必要地设置了太多的对象''= Nothing''?

3.重命名声明似乎像VB6一样 - 是不是有文本流

重命名?


我想尽可能多地使用vb.net方式,但似乎在搜索网络的时候我用VB6,Net2003和

Net2005

代码。

。 。 ..
Hello All,

I''d like your comments on the code below. The sub does exactly what I
want it to do but I don''t feel that it is solid as all. It seems like I''m
using some VB6 code, .Net2003 code, and .Net2005 code. I''m developing in
vb.net 2005.

This test sub just reads an input text file, writing out records to
another text file, eliminating records that have a ''99'' in them (it is
similar to
a CSV file).

Some of my concerns are:

1, ''writeline'' does not convert to upper/lower case when keyed in - am I
missing a reference?
2. Am I unnecessarily setting too many objects to ''= Nothing''?
3. The ''Rename'' statement seems like VB6 - isn''t there a textstream
rename?

I want to do this the vb.net way as much as possible, but it seems that
while searching the net that I wind up with a mix of VB6, Net2003 and
Net2005
code.
. . ..



你对这段代码不满意是绝对正确的。这是非常好看的b $ b看起来老而且非常草率。 VB.NET可以做得更好。首先是转向

Option Explicit和Option Strict on。你会发现第一件事是你b $ b既有TS0(零)又有TSO(大写'o'')。选项明确关闭

允许这个,这就是为什么它是令人厌恶的原因。接下来是摆脱你的
前向声明和COM互操作并使用.NET框架类

IO。


结果如下:


选项明确开启

选项严格开启


Imports System.IO


模块模块1

Private Sub RemoveRecs()

''**************** ********************************* ***************** ***********

''这个例程读取.pgh文件,并删除任何记录有

a''99''
''在其中。创建一个输出文件(没有99'),原始的

文件被删除,并且新文件被重命名为原始文件。

''******************************************* ***** ****************************


Dim AnyChanges As Boolean = False

Dim InFileName As String =" C:\ _jjj.txt"

Dim OutFileName As String =" c:\kkk.txt"


使用TSI作为新的StreamReader(InFileName)

使用TS0作为新的StreamWriter(OutFileName)

请勿使用TSI.EndOfStream

Dim strRecord As String = TSI.ReadLine()


如果strRecord.Contains(" 99")那么

AnyChanges = True

Else

TS0.WriteLine(strRecord)

结束如果

循环

TS0.Close()

结束使用

TSI.Close()

结束使用


如果AnyChanges那么

File.Delete(InFileName)

File.Move(O utFileName,InFileName)

Else

File.Delete(OutFileName)

结束如果


结束Sub $ / $
结束模块


David

You were absolutely right to be dissatisfied with this code. It''s very
old-looking and very sloppy. VB.NET can do much better. First is to turn
Option Explicit and Option Strict on. First thing you will find is that you
had both TS0 (with zero) and TSO (with capital ''o''). Option Explicit Off
allows this, which is why it''s an abomination. Next is to get rid of your
forward declarations and the COM interop and use .NET framework classes for
IO.

Resulting in something like:

Option Explicit On
Option Strict On

Imports System.IO

Module Module1
Private Sub RemoveRecs()
''************************************************* ****************************
'' This routine reads the .pgh file and eliminates any records that have
a ''99''
'' in them. An output file is created (without the 99''s), the original
file is
'' deleted and the new file is renamed to the original.
''************************************************* ****************************

Dim AnyChanges As Boolean = False
Dim InFileName As String = "C:\jjj.txt"
Dim OutFileName As String = "c:\kkk.txt"

Using TSI As New StreamReader(InFileName)
Using TS0 As New StreamWriter(OutFileName)
Do While Not TSI.EndOfStream
Dim strRecord As String = TSI.ReadLine()

If strRecord.Contains("99") Then
AnyChanges = True
Else
TS0.WriteLine(strRecord)
End If
Loop
TS0.Close()
End Using
TSI.Close()
End Using

If AnyChanges Then
File.Delete(InFileName)
File.Move(OutFileName, InFileName)
Else
File.Delete(OutFileName)
End If

End Sub
End Module

David


Hexman写道:
Hexman wrote:

FSO =没什么''清理 - 销毁物品

FSO1 =没什么

TSI =没什么

TSO = Nothing
FSO = Nothing ''Clean up - destroy objects
FSO1 = Nothing
TSI = Nothing
TSO = Nothing



仅供参考,在.Net中将COM对象设置为Nothing

不清理它。对于COM对象,你必须调用

Marshal.ReleaseComObject(FSO)。

And just for reference, in .Net setting a COM object to Nothing does
not clean it up. For COM objects you must call
Marshal.ReleaseComObject(FSO).


谢谢你们,David&克里斯,


你的代码写得很好,简洁!很好的例子!


我肯定会对明确的选项提出建议。严格和前瞻性声明。至于COM互操作,我将不得不看看真正的b $ b是多少(我没有深入的VB背景)。至于.Net Framework(2005),似乎我在Borders浏览的所有书籍都混合了VB6,.Net 2003和

..Net 2005.

如果您知道任何.Net 2005特定书籍/网站/等。这只会告诉我.Net 2005的方式,请告诉我。另外一件事是,我不会阅读技术书籍封面。我通常会找到一个适用于手头任务的章节,并尝试使他们的解决方案适应我的代码。

(也许我最好先开始阅读架构部分!!!)


再次感谢,

Hexman

2006年11月22日星期三06:45:31 -0600,大卫布朗" < davidbaxterbrowne没有盆栽 me**@hotmail.com 写道:
Thank you both, David & Chris,

Your code is well written and concise! Great example!

I''ll certainly take your advice on options Explicit & Strict and the forward declarations. As for the COM interop, I''ll have to see what that really
is (I don''t have a deep background in VB). As for .Net Framework (2005), it seems like all the books I''ve browsed at Borders mix VB6, .Net 2003, and
..Net 2005.

If you know of any .Net 2005 specific books/websites/etc. that will show me only the .Net 2005 way, please let me know. The one other thing is that I
don''t read technical books cover to cover. I usually find a chapter that applies to the task at hand and try to adapt their solutions to my code.
(Maybe I better start reading the "Architecture" parts first!!!)

Thanks again,

Hexman
On Wed, 22 Nov 2006 06:45:31 -0600, "David Browne" <davidbaxterbrowne no potted me**@hotmail.comwrote:

>

" Hexman" <他**** @ binary.com写了留言
新闻:f8 ****************************** **@4ax.com ..
>

"Hexman" <He****@binary.comwrote in message
news:f8********************************@4ax.com.. .

>大家好,

我想要你对以下代码的评论。 sub确实完成了我想要它做的事情,但我觉得它并不像所有人那样坚固。看起来我正在使用一些VB6代码,.Net2003代码和.Net2005代码。我正在开发
vb.net 2005.

这个测试子只是读取一个输入文本文件,将记录写入另一个文本文件,从而消除了具有'99'在其中(它与
CSV文件类似)。

我的一些担忧是:

1,'键入时'writeline''不会转换为大写/小写 - 我是否缺少参考?
2。我是否不必要地设置了太多的对象''= Nothing''?
3。 ''Rename''语句好像是VB6 - 是不是有一个文本流重命名?

我想尽可能地用vb.net这样做,但似乎那个搜索网络的时候我用VB6,Net2003和
Net2005代码混合起来。
。 。 ..
>Hello All,

I''d like your comments on the code below. The sub does exactly what I
want it to do but I don''t feel that it is solid as all. It seems like I''m
using some VB6 code, .Net2003 code, and .Net2005 code. I''m developing in
vb.net 2005.

This test sub just reads an input text file, writing out records to
another text file, eliminating records that have a ''99'' in them (it is
similar to
a CSV file).

Some of my concerns are:

1, ''writeline'' does not convert to upper/lower case when keyed in - am I
missing a reference?
2. Am I unnecessarily setting too many objects to ''= Nothing''?
3. The ''Rename'' statement seems like VB6 - isn''t there a textstream
rename?

I want to do this the vb.net way as much as possible, but it seems that
while searching the net that I wind up with a mix of VB6, Net2003 and
Net2005
code.
. . ..


你对这段代码不满意是完全正确的。这看起来非常古老而且非常草率。 VB.NET可以做得更好。首先是转向
Option Explicit和Option Strict。你会发现第一件事是你有TS0(零)和TSO(大写'o'')。 Option Explicit Off
允许这个,这就是为什么它是一个令人厌恶的原因。接下来是摆脱你的前向声明和COM互操作并使用.NET框架类来实现IO。

结果如下:
Option Explicit On
选项严格开启

进口System.IO

模块模块1


私有Sub RemoveRecs()

''**************************************** ********* ****************************

''此例程读取.pgh文件并删除其中包含'99''

''的所有记录。创建一个输出文件(没有99'),原始的

''文件被删除,新文件被重命名为原始文件。

''*********************************************** ** ****************************


Dim AnyChanges As Boolean = False

Dim InFileName As String =" C:\ jjj.txt"

Dim OutFileName As String =" c:\kkk.txt"


使用TSI作为新的StreamReader(InFileName)

使用TS0作为新的StreamWriter(OutFileName)

做的不是TSI.EndOfStream

Dim strRecord As String = TSI.ReadLine()


如果strRecord.Contains(" 99")那么

AnyChanges = True

Else

TS0.WriteLine(strRecord)

结束如果

循环

TS0.Close( )

结束使用

TSI.Close()

结束使用


如果AnyChanges那么

File.Delete(InFileName)

File.Move(OutFileName,InFileName)

Else

File.Delete(OutFileName)

结束如果


End Sub
结束模块

大卫


You were absolutely right to be dissatisfied with this code. It''s very
old-looking and very sloppy. VB.NET can do much better. First is to turn
Option Explicit and Option Strict on. First thing you will find is that you
had both TS0 (with zero) and TSO (with capital ''o''). Option Explicit Off
allows this, which is why it''s an abomination. Next is to get rid of your
forward declarations and the COM interop and use .NET framework classes for
IO.

Resulting in something like:

Option Explicit On
Option Strict On

Imports System.IO

Module Module1
Private Sub RemoveRecs()
''************************************************* ****************************
'' This routine reads the .pgh file and eliminates any records that have
a ''99''
'' in them. An output file is created (without the 99''s), the original
file is
'' deleted and the new file is renamed to the original.
''************************************************* ****************************

Dim AnyChanges As Boolean = False
Dim InFileName As String = "C:\jjj.txt"
Dim OutFileName As String = "c:\kkk.txt"

Using TSI As New StreamReader(InFileName)
Using TS0 As New StreamWriter(OutFileName)
Do While Not TSI.EndOfStream
Dim strRecord As String = TSI.ReadLine()

If strRecord.Contains("99") Then
AnyChanges = True
Else
TS0.WriteLine(strRecord)
End If
Loop
TS0.Close()
End Using
TSI.Close()
End Using

If AnyChanges Then
File.Delete(InFileName)
File.Move(OutFileName, InFileName)
Else
File.Delete(OutFileName)
End If

End Sub
End Module

David


这篇关于使用FSO,TextStream等所需的帮助---请求代码审查和建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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