需要帮助,删除文本文件中的重复行 [英] Help needed, Removing duplicate lines in text file

查看:66
本文介绍了需要帮助,删除文本文件中的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我会首先解释一下我的应用程序的功能,以免在我问我问题时不要惹你生气。 a?o


我有一个VB.Net 2.0应用程序启动一个进程(process.start ...)和

传递一个参数来自组合框。组合框项目是由IP地址和计算机主机名组成的
。用户放置的任何内容

这个组合框将它写入一个名为history.txt的txt文件


My.Computer.FileSystem.WriteAllText(Application.Us erAppDataPath +

" \ History.txt",Me.ComboBox1.Text + vbCrLf,True)


重启应用程序时(关闭并打开) )它清除了

组合框项目(me.combobox1.items.clear()),然后从

导入上面的history.txt文件。


但是我遇到的问题是使用的是公共地址。 (即

192.168.0.1)这些在列表中显示4或5次,因为它们已被用户输入4到5次组合框中。$ br />

在form1.load上有没有读取这个文本文件并删除任何具有相同地址的

行?


如果您需要更多信息,请告诉我。

所有和任何帮助都会很好。


问候,

Andy

Hello all,

I''ll start by explaining what my app does so not to confuss you when i
ask my question. a?o

I have a VB.Net 2.0 app that starts a process (process.start ...) and
passes a prameter through from a combo box. The combo box items are
made up of IP address and computer host name. Anything a user places in
this combo box it writes this to a txt file called history.txt

My.Computer.FileSystem.WriteAllText(Application.Us erAppDataPath +
"\History.txt", Me.ComboBox1.Text + vbCrLf, True)

When the application is restarted (closed and opend) its clears the
combo box items (me.combobox1.items.clear()) and then imports them from
the above history.txt file.

However the problem i have is with common address''s that are used. (ie
192.168.0.1) these show in the list 4 or 5 times as they have been
typed into the combo box 4 -5 times by the user.

Is there anyway on form1.load to read this text file and remove any
line that has the same address?

If you need any more info please let me know.
All and any help would be nice.

Regards,

Andy

推荐答案

您好,Andy,


有不同如何处理这个问题。 最好的方式可能取决于

你现在加载你的组合框的方式(和用什么类型的对象)。


在早期的案例中,我做了一些事情类似的创建一个类

,它抑制了重复的条目,并使用它来加载列表。


但在你的情况下,简单地检查每个条目可能更好在

History.txt之前将它放入你的ComboBox列表中,看看它是否已经存在。


如果可以的话给出一些详细信息(例如代码片段),显示你当前如何加载你的列表,你可能得到一个更好的答案。


干杯,

兰迪

也许你可以最好地过滤


Backwards写道:
Hello, Andy,

There are different ways to approach this. The "best" way may depend on
how (and with what type of objects) you are now loading your combo-box.

In an earlier case I have done something similar by creating a class
that suppressed duplicate entries and used that to load the list.

But in your case it might be better simply to examine each entry in
History.txt before you place it into your ComboBox list to see if it
already exists.

If you can give some details (e.g. a code snippet) showing how you are
currently loading your list, you might get a better answer.

Cheers,
Randy
Perhaps you can best filter the

Backwards wrote:
大家好,

我首先会解释我的应用程序的功能,以便在我提出问题时不要迷惑你。我有一个启动进程的VB.Net 2.0应用程序(process.start ...)和
从组合框传递一个参数。组合框项目由IP地址和计算机主机名组成。用户放置在这个组合框中的任何内容都会将其写入名为history.txt的txt文件中。

My.Computer.FileSystem.WriteAllText(Application.Us erAppDataPath +
" \ History.txt",Me.ComboBox1.Text + vbCrLf,True)

当应用程序重新启动(关闭并打开)时,它会清除
组合框项目(me.combobox1。 items.clear())然后从
上面的history.txt文件中导入它们。

然而我遇到的问题是使用的是公共地址。 (即
192.168.0.1)这些在列表中显示4或5次,因为它们已经被用户输入组合框4到5次。

有没有无论如何在form1.load上读取这个文本文件并删除任何具有相同地址的
行?

如果您需要更多信息,请告诉我。
所有和任何帮助会很好。

问候,

Andy
Hello all,

I''ll start by explaining what my app does so not to confuss you when i
ask my question. a?o

I have a VB.Net 2.0 app that starts a process (process.start ...) and
passes a prameter through from a combo box. The combo box items are
made up of IP address and computer host name. Anything a user places in
this combo box it writes this to a txt file called history.txt

My.Computer.FileSystem.WriteAllText(Application.Us erAppDataPath +
"\History.txt", Me.ComboBox1.Text + vbCrLf, True)

When the application is restarted (closed and opend) its clears the
combo box items (me.combobox1.items.clear()) and then imports them from
the above history.txt file.

However the problem i have is with common address''s that are used. (ie
192.168.0.1) these show in the list 4 or 5 times as they have been
typed into the combo box 4 -5 times by the user.

Is there anyway on form1.load to read this text file and remove any
line that has the same address?

If you need any more info please let me know.
All and any help would be nice.

Regards,

Andy



嗨除了Randy,


如果有可能我宁愿将地址历史记录保存在表格中,那么

它很容易选择不同的... 。

但您也可以对文本文件进行选择,例如:

假设这是您的文本文件:

addresses.txt


ADDRESS

" 192.168.1.1"

" 192.168.1.1"

" 192.168.1.3"

" 192.168.1.5"

" 192.168.1.5"


需要包含地址,它是列标题,你也可以这样做

没有列标题,但我的样本使用它。


Dim myCon作为新的OleDbConnection(" Provider) = Microsoft.Jet.OLEDB.4.0;数据

来源=" &安培; Application.StartupPath& " \" &安培; " ;; Extended

Properties ="" text; HDR = Yes; FMT = Delimited""")

Dim myAdapter As New OleDbDataAdapter(" Select)来自

addresses.txt",myCon)的明确地址

将我的数据集视为新数据集


myCon.Open()

myAdapter.Fill(myDataset)

myCon.Close()


For i As Integer = 0 to myDataset.Tables(0) .Rows.Count - 1

ComboBox1.Items.Add(CStr(myDataset.Tables(0).Rows(i).Item(0)))

Next

希望这会有所帮助


彼得


-

今天的编程是比赛软件工程师之间努力建立更大和更好的防止白痴程序,并且宇宙试图产生更大和更好的白痴。到目前为止,宇宙正在赢得胜利。 (Rich Cook)


" R。麦克唐纳" < SC **** @ NO-SP-AMcips.ca> schreef in bericht

news:44 *********************** @ news.wanadoo.nl ...
Hi in addition to Randy,

If it''s possible I would prefer to save the address history in a table then
it''s very easy to do select distinct ....
But you can also do a select on a text file, for example:
Assuming this is your text file:
addresses.txt

ADDRESS
"192.168.1.1"
"192.168.1.1"
"192.168.1.3"
"192.168.1.5"
"192.168.1.5"

ADDRESS needs to be included, it''s the columnheader, you can also do it
without a columnheader but my sample uses it.

Dim myCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=" & Application.StartupPath & "\" & ";Extended
Properties=""text;HDR=Yes;FMT=Delimited""")
Dim myAdapter As New OleDbDataAdapter("Select distinct address from
addresses.txt", myCon)
Dim myDataset As New DataSet

myCon.Open()
myAdapter.Fill(myDataset)
myCon.Close()

For i As Integer = 0 To myDataset.Tables(0).Rows.Count - 1
ComboBox1.Items.Add(CStr(myDataset.Tables(0).Rows( i).Item(0)))
Next
Hope this helps

Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"R. MacDonald" <sc****@NO-SP-AMcips.ca> schreef in bericht
news:44***********************@news.wanadoo.nl...
您好,Andy,

有不同的方法来解决这个问题。 最好的方式可能取决于你现在加载组合框的方式(以及用什么类型的对象)。

在早期的案例中,我通过创建类来做类似的事情。 >它抑制了重复的条目,并使用它来加载列表。

但在你的情况下,最好只是检查
History.txt中的每个条目,然后再将它放入你的ComboBox列表看看它是否已经存在。

如果您能提供一些详细信息(例如代码片段),显示您目前如何加载列表,您可能会得到更好的答案。

干杯,
兰迪

也许你最能过滤掉

Backwards写道:
Hello, Andy,

There are different ways to approach this. The "best" way may depend on
how (and with what type of objects) you are now loading your combo-box.

In an earlier case I have done something similar by creating a class
that suppressed duplicate entries and used that to load the list.

But in your case it might be better simply to examine each entry in
History.txt before you place it into your ComboBox list to see if it
already exists.

If you can give some details (e.g. a code snippet) showing how you are
currently loading your list, you might get a better answer.

Cheers,
Randy
Perhaps you can best filter the

Backwards wrote:

我会首先解释一下我的应用程序的功能,以免在我提问时提出问题。 ?

我有一个VB.Net 2.0应用程序,它启动一个进程(process.start ...)并且
从一个组合框传递一个参数。组合框项目由IP地址和计算机主机名组成。用户放置在这个组合框中的任何内容都会将其写入名为history.txt的txt文件中。

My.Computer.FileSystem.WriteAllText(Application.Us erAppDataPath +
" \ History.txt",Me.ComboBox1.Text + vbCrLf,True)

当应用程序重新启动(关闭并打开)时,它会清除
组合框项目(me.combobox1。 items.clear())然后从
上面的history.txt文件中导入它们。

然而我遇到的问题是使用的是公共地址。 (即
192.168.0.1)这些在列表中显示4或5次,因为它们已经被用户输入组合框4到5次。

有没有无论如何在form1.load上读取这个文本文件并删除任何具有相同地址的
行?

如果您需要更多信息,请告诉我。
所有和任何帮助会很好。

问候,

Andy
Hello all,

I''ll start by explaining what my app does so not to confuss you when i
ask my question. ?

I have a VB.Net 2.0 app that starts a process (process.start ...) and
passes a prameter through from a combo box. The combo box items are
made up of IP address and computer host name. Anything a user places in
this combo box it writes this to a txt file called history.txt

My.Computer.FileSystem.WriteAllText(Application.Us erAppDataPath +
"\History.txt", Me.ComboBox1.Text + vbCrLf, True)

When the application is restarted (closed and opend) its clears the
combo box items (me.combobox1.items.clear()) and then imports them from
the above history.txt file.

However the problem i have is with common address''s that are used. (ie
192.168.0.1) these show in the list 4 or 5 times as they have been
typed into the combo box 4 -5 times by the user.

Is there anyway on form1.load to read this text file and remove any
line that has the same address?

If you need any more info please let me know.
All and any help would be nice.

Regards,

Andy



这是我的表单加载上的代码从history.txt中提取信息

Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs )处理MyBase.Load

尝试

Me.ComboBox1.Items.Clear()


如果不是File.Exists(应用程序) .UserAppDataPath +

" \ History.txt")然后

MessageBox.Show("找不到历史文件,没有历史记录

将显示,Application.ProductName,MessageBoxButtons.OK,

MessageBoxIcon.Informat离开)

''返回

结束如果

使用sr作为StreamReader =

File.OpenText(应用程序。 UserAppDataPath +" \ History.txt")

Dim input As String

input = sr.ReadLine()

虽然未输入是没有什么

''MessageBox.Show(输入)

Me.ComboBox1.Items.Add(输入)

input = sr.ReadLine()

结束时

''MessageBox.Show("流的结尾已经达到了
。)

sr.Close()

结束使用

Catch ex As Exception

MessageBox.Show(ex.Message," Error" ;

This is my code on form load that pulls the info from the history.txt

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Me.ComboBox1.Items.Clear()

If Not File.Exists(Application.UserAppDataPath +
"\History.txt") Then
MessageBox.Show("History file was not found, no histroy
will be showen", Application.ProductName, MessageBoxButtons.OK,
MessageBoxIcon.Information)
''Return
End If
Using sr As StreamReader =
File.OpenText(Application.UserAppDataPath + "\History.txt")
Dim input As String
input = sr.ReadLine()
While Not input Is Nothing
''MessageBox.Show(input)
Me.ComboBox1.Items.Add(input)
input = sr.ReadLine()
End While
''MessageBox.Show("The end of the stream has been
reached.")
sr.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "Error"


这篇关于需要帮助,删除文本文件中的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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