删除“双"文本文件中的一行 [英] remove "double" line from Textfile

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

问题描述

我的项目快完成了!
现在,我有一个问题:

我的代码正在逐行读取,结果以另一种形式显示在我的应用程序中.没关系,但是只有一件事我不知道该如何改变.

My project is almost done!
For now I have this question:

My code is reading line by line and the result is showing in another form within my app. That''s ok, but only one thing that I don''t know how to change.

Case "TEL;WORK;VOICE"                               'Telefoon op het werk
                            strTelWerk = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)




上面的代码:文件中的"TEL; WORK..etc"行是文件的2倍. 现在,我想删除该行(如果该行已经在该文件中)..如果该行存在,则删除并转到下一行.

这是代码:




The Code above: The line "TEL;WORK..etc is 2 times in the file..
Now I want to delete the line if the line is in that file already.. so if the line exist than delete and go to next line.

This is the code:

Private Sub Importeren() _
        Handles itemImporteerVCard.Click, mnuImportVCard.Click, btnImportVCard.Click 'Alle knoppen voor het importeren.

        '------------------------------------------------------------------------
        ' Onderstaande code doet het wel, maar ik moet nog wat dingen uitzoeken.
        'Mijn originele code:
        '------------------------------------------------------------------------
        Dim strBestand As String = String.Empty                 'Locatie + naam van het bestand
        Dim strRegel As String = String.Empty                   'Regel/lijn
        Dim strWaarde() As String = Nothing                     'de inhoud van de regels/lijnen

        Dim objReader As System.IO.StreamReader = Nothing, sbBuilder As System.Text.StringBuilder = Nothing


        'Variabelen:
        '--------------------------------
        Dim strNaam As String = ""
        Dim strVoornaam As String = ""
        Dim strTelWerk As String = ""
        Dim strTelThuis As String = ""
        Dim strMobiel As String = ""
        Dim strOverig As String = ""
        Dim strFaxWerk As String = ""
        Dim strFaxThuis As String = ""
        Dim strAdres As String = ""
        Dim strUrlThuis As String = ""
        Dim strUrlWerk As String = ""
        Dim strMail As String = ""

        Dim boolFlag As Boolean = false ' *****


        'Bevestiging voor het importeren:
        If MsgBox("Wilt u een v-Card Importeren?", 36, "Bevestiging voor het importeren") = MsgBoxResult.Yes Then
            Try
                strBestand = "C:\Test.vcf"                                  'Verwijst naar de locatie van het bestand
                sbBuilder = New System.Text.StringBuilder                   'Er is een nieuwe stringbuilder gemaakt

                frmVCard.txtVCard.Text = strBestand                         'frmVcard.txtVcard.Text moet gevuld worden met de inhoud van het bestand
                objReader = New System.IO.StreamReader(strBestand)

                'Eigenschappen voor het openen van de V-Card:
                ofdImport.Filter = "V-Cards (*.vcf)|*.vcf"                  'Filter betreft de extensie van het bestand *.vcf
                ofdImport.FileName = "*.vcf"                                'Bestandsnaam: alles.vcf
                ofdImport.Title = "V-Card importeren"                       'Titel van het dialoogvenster voor het openen

                If ofdImport.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                    objReader = New System.IO.StreamReader(strBestand)

                    frmVCard.Show()                                         'Laat de form zien waar de Vcard in terecht komt
                End If

                Do While objReader.Peek >= 1
                    strRegel = objReader.ReadLine()                         'Elke regel/lijn wordt 1 voor 1 gelezen
                    strWaarde = strRegel.Split(":")                         'De waarde van elke regel wordt gesplits d.m.v. ":"

                    Select Case UCase(strWaarde(0))                         '(0) = het gedeelte voor ":" en (1) komt daarna

                        Case "BEGIN", "VERSION", "END"                      'De waarden die niet worden getoond.

                        Case "N"                                            'Naam
                            strNaam = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)

                        Case "FN"                                           'Voornaam
                            strVoornaam = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "TEL;WORK;VOICE"                               'Telefoon op het werk
                            If boolFlag = false  ' *****
                            Then                 ' *****
                                strTelWerk = strWaarde(1)
                                sbBuilder.Append(strWaarde(1) & vbTab)
                                boolFlag = true  ' *****
                            End If               ' *****

                        Case "TEL;HOME;VOICE"                               'Telefoon thuis
                            strTelThuis = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "TEL;CELL;VOICE"                               'Mobiele telefoon
                            strMobiel = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)

                        Case "TEL;VOICE"                                    'Overige nummers
                            strOverig = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "TEL;WORK;FAX"                                 'Fax op het werk
                            strFaxWerk = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)

                        Case "TEL;FAX"                                      'Fax thuis
                            strFaxThuis = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "ADR;WORK;PREF:"                               'Adres
                            strAdres = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "URL;HOME:"                                    'Website Thuis
                            strUrlThuis = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)

                        Case "URL;WORK:"                                    'Website Werk
                            strUrlWerk = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "EMAIL;PREF;INTERNET:"                         'Email adres
                            strMail = strWaarde(1)
                            sbBuilder.Append(strWaarde(1))

                        Case Else
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                    End Select
                Loop
                objReader.Close()
                frmVCard.txtVCard.Text = sbBuilder.ToString

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fout...")

            Finally
                objReader.Dispose()
                sbBuilder = Nothing
            End Try
        End If
    End Sub


我希望任何人都可以帮助我.


I hope anyone can help me out.
Thanks already!

推荐答案

使用标记或其他元素并将其设置为false.然后在case子句中检查标志是否为false,如果是,则保存信息并将标志设置为true.如果该标志为true,则不会发生任何事情,因此您将忽略重复项.
Use a flag or some other element and set it to false. Then within your case clause check if the flag is false, and if so save the information and set the flag to true. If the flag is true, nothing will happen so you ignore the duplicate.


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

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