Results 1 to 10 of 10

Thread: Erase a single line from a text file?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    40

    Erase a single line from a text file?

    Hey all,

    I am writing a program that protects selected folders of your choice with a password. Going great so far, I was almost done, then I ran in to a problem.
    The last thing that I need to complete is this:
    I want the end user to be able to delete a directory. I am using a simple text file (that I change the extension to .dll for security purposes) to store the information of which directories to protect. The program reads from that file line by line to deterime which directories to protect.
    There is a lot of code so I won't post it all, but I will post the section that I am working on.
    Note, in the code that I have figured out how to match and find the line that I want to delete, I just don't know how to delete it.
    VB Code:
    1. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    2.  
    3.         Dim fcontent() As String = Split(My.Computer.FileSystem.ReadAllText("C:\Program Files\Folder Protection\fld.dll"), vbCrLf) 'file from which to read text
    4.         For a As Integer = 0 To UBound(fcontent) 'reads the array of text lines
    5.             If String.Compare(fcontent(a), TextBox1.Text) = 0 Then  'compares the lines of text to the text in textbox1
    6.                       'CODE TO DELETE LINE HERE!!!
    7.             Else : MsgBox("Directory not found!")
    8.             End If
    9.         Next
    10.  
    11.     End Sub
    Thank You,
    Michael Ifland
    Visual Basic.NET 2005

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Erase a single line from a text file?

    I would recomend insead of using the text file withany extention which can be opened with texteditor use My.Settings to save the folders name's.
    Back to your original question you can ues File.ReadAllLines to get a string array of the lines close the file and delete it and create a new file write all lines from the string array excluding the ones you need to delete and close the file.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    40

    Re: Erase a single line from a text file?

    Hello VBDT,

    Thanks for the quick reply.
    I understand how to read the lines of the file. But how do I rewrite the new file without the line that I don't want? I understand the concept of what you are describing, but what other commands or code do I use to do such a task.
    Thanks, in advance for your help.
    Thank You,
    Michael Ifland
    Visual Basic.NET 2005

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Erase a single line from a text file?

    Read the file into an array (File.ReadAllLines). Load the array into a collection (New List(Of String)). Remove the lines you don't want (List.RemoveAt). Extract an array from the collection (List.ToArray). Write the array to the file (File.WriteAllLines).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Erase a single line from a text file?

    Quote Originally Posted by m_s_ifland
    Hello VBDT,

    Thanks for the quick reply.
    I understand how to read the lines of the file. But how do I rewrite the new file without the line that I don't want? I understand the concept of what you are describing, but what other commands or code do I use to do such a task.
    Thanks, in advance for your help.
    Is there any particular reason you don’t want to use the application settings? It is more suitable I think! If there is then this is how you can do with the file. Just use this sub.
    VB Code:
    1. Private Sub RemoveFolders(ByVal fileName As String, ByVal folders() As String)
    2.         Dim lines() As String = IO.File.ReadAllLines(fileName)
    3.         Dim sw As New IO.StreamWriter(fileName)
    4.         For Each line As String In lines
    5.             If Array.IndexOf(folders, line) = -1 Then
    6.                 sw.WriteLine(line)
    7.             End If
    8.         Next
    9.         sw.Close()
    10.         sw.Dispose()
    11.     End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    40

    Re: Erase a single line from a text file?

    Yes actually there is a reason,

    I don't know how, I'm a noob.
    Can I get a crash course or good tutorial?
    I do appreciate you help, thus far.
    Thank You,
    Michael Ifland
    Visual Basic.NET 2005

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    40

    Re: Erase a single line from a text file?

    Again thanks for you help. I have another question, I've used your code and when I call on the sub, I'm not quite sure how to use it. Of course I know what the "fileName" is, I think(the path to the file). But I tried putting in the textbox1.text from where the folder path will be and it tells me:
    "Value of type 'String' cannot be converted to '1-Dimensional array of String'.
    I don't know what to do.
    Thank You,
    Michael Ifland
    Visual Basic.NET 2005

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Erase a single line from a text file?

    VB Code:
    1. 'Call the sub like this. Note the folders parameter is an array of strings so you
    2. 'can pass more than one folders to be removed. This call only removes one.
    3. Me.RemoveFolders("file path", New String() {Me.TextBox1.Text})
    To use the app Settings you should add a System.Collections.Specialized.StringCollection property with user scope like it is shown in the image. And use these lines of code to add and remove folder paths. Next time your app starts the folders will be accessible.
    VB Code:
    1. 'Add folder
    2. My.Settings.Folders.Add("folder path to add")
    3. 'Remove folder
    4. My.Settings.Folders.Remove("folder path to remove")
    Attached Images Attached Images  

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    40

    Re: Erase a single line from a text file?

    Ok that works(Me.RemoveFolders("file path", New String() {Me.TextBox1.Text})), except one thing. I need it to also remove the "vbCrLf" at the end. I tried a couple of things, like adding it to the end of the code above. It let me put it in there without errors, but it didn't work.
    If this is in there it reads it as a blank string and it affects the operation of the whole program. Please help.
    Thank you very much for your help so far, it is very appreciated.
    Thank You,
    Michael Ifland
    Visual Basic.NET 2005

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Erase a single line from a text file?

    I am not sure why you have it in there in a first place! It is a vb6 function and it is not a new line by its self; don’t use vb6 functions in .Net use "Environment.NewLine" or "ControlChars.NewLine" if you want new line to insert in to a string. Now, you don’t need to add new lines when you write in to file use “WriteLine” method of the stream writer like this:
    VB Code:
    1. 'Write folder paths in a file.
    2.         Dim folders() As String = {"folder1", "folder2", "..."}
    3.         Using sw As New IO.StreamWriter(fileName)
    4.             For Each folder As String In folders
    5.                 'This line writes a line in to
    6.                 'the file with null at the end.
    7.                 sw.WriteLine(folder)
    8.             Next
    9.         End Using
    10.  
    11.         'Or if you want to append a folder to a file use this.
    12.         Using sw As New IO.StreamWriter(fileName, True)
    13.             'This line writes a line in to
    14.             'the file with null at the end.
    15.             sw.WriteLine(folder)
    16.         End Using
    You shoulden't wory about newlins any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width