Results 1 to 12 of 12

Thread: Deleting a txt file

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    32

    Deleting a txt file

    For example I have a open file dialog, which opens a txt file.

    When I press a specific button, it clears the whole text file.

    How do I do that?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Deleting a txt file

    What application do you open the text file with? If it's displayed in a textbox or richtextbox in your own application then you just clear the textbox (or richtextbox).
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    32

    Re: Deleting a txt file

    I did not show any of the content from the text file in any controls. The result is that the text file is now blank.

  4. #4
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Deleting a txt file

    So you don't actually want to delete the whole file, just remove it's contents?

    vb.net Code:
    1. Dim OFD As New OpenFileDialog
    2.         OFD.Filter = ".txt Files (*.txt)|*.txt" 'might have done the filter wrong, i always forget :P
    3.  
    4.         If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
    5.             Dim finfo As New IO.FileInfo(OFD.FileName)
    6.             If finfo.Extension.ToLower = ".txt" Then
    7.                 IO.File.Delete(OFD.FileName)
    8.                 IO.File.Create(OFD.FileName)
    9.             End If
    10.         End If

    There are numerous ways to do this, this one just deletes the existing file and creates a new one in place of it. You could also use streamreader set on "append - False" and write nothing to the file or whatever you want really.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    32

    Re: Deleting a txt file

    Will I be able to write that file afterwards?

    Im afraid the error "file being used by other" will show up.

  6. #6
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Deleting a txt file

    yes you will be able to write to the file, just try it out and see!

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    32

    Re: Deleting a txt file

    Code:
    Dim OFD As New OpenFileDialog
    OFD.Filter = ".txt Files (*.txt)|*.txt"
    
            If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim finfo As New IO.FileInfo(OFD.FileName)
                If finfo.Extension.ToLower = ".txt" Then
                    IO.File.Delete(OFD.FileName)
                    IO.File.Create(OFD.FileName)
                End If
            End If
    
    Dim Write As New System.IO.Streamwriter(OFD.Filename)
    
    Write.WriteLine("Test Line")
    I typed that in and it gave me the "file is being used in other process" error.

  8. #8
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Re: Deleting a txt file

    Code:
    Dim Write As New System.IO.Streamwriter(OFD.Filename)
    
    Write.WriteLine("Test Line")
    
    Write.Close()
    Dont forget to close the stream!
    Life runs on code

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    32

    Re: Deleting a txt file

    ah ok it seems I've resolved the question.

    Thanks guys!

  10. #10
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Deleting a txt file

    Code:
    Dim OFD As New OpenFileDialog
    OFD.Filter = ".txt Files (*.txt)|*.txt"
    
            If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim finfo As New IO.FileInfo(OFD.FileName)
                If finfo.Extension.ToLower = ".txt" Then
                    IO.File.Delete(OFD.FileName)
                    IO.File.Create(OFD.FileName)
                    Using sWrite As New IO.Streamwriter(OFD.Filename)
                           sWrite.WriteLine("Test Line")
                    End Using
                End If
            End If
    Try that out, I changed up some stuff and moved your streamwriter argument. Are you sure you dont have the txt file open somewhere at the time you're trying to delete it?

  11. #11
    Hyperactive Member Skatebone's Avatar
    Join Date
    Jan 2009
    Location
    Malta
    Posts
    279

    Re: Deleting a txt file

    Ps: that is too complicated

    Here I simplified a little so that you can learn from it:

    Code:
            Dim OFD As New OpenFileDialog
    
            OFD.Filter = ".txt Files (*.txt)|*.txt"
    
            If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim finfo As New IO.FileInfo(OFD.FileName)
                If finfo.Extension.ToLower = ".txt" Then
                    'If File exists, delete it.
                    If FileIO.FileSystem.FileExists(OFD.FileName) = True Then
                        Kill(OFD.FileName)
                    End If
                    'Write your data
                    Dim Write As New System.IO.StreamWriter(OFD.FileName)
                    Write.WriteLine("Test Line")
                    Write.Close()
    
                End If
            End If
    Life runs on code

  12. #12
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Deleting a txt file

    Okay fixed it so much easier!

    vb.net Code:
    1. Dim OFD As New OpenFileDialog
    2.             OFD.Filter = ".txt Files (*.txt)|*.txt"
    3.  
    4.             If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
    5.                 Dim fInfo As New IO.FileInfo(OFD.FileName)
    6.                 If fInfo.Extension.ToLower = ".txt" Then
    7.  
    8.                     Dim fStream As New IO.FileStream(OFD.FileName, IO.FileMode.Truncate)
    9.                     fStream.Close()
    10.                     Using sWrite As New IO.StreamWriter(OFD.FileName)
    11.                         sWrite.WriteLine("Test Line")
    12.                     End Using
    13.                 End If
    14.             End If

    Oh yeah, if what you're trying to do is clear the text file so that you can start adding lines from scratch, the simplest way to do this is very simple:

    vb.net Code:
    1. Dim OFD As New OpenFileDialog
    2.         OFD.Filter = ".txt Files (*.txt)|*.txt"
    3.  
    4.         If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
    5.             Dim fInfo As New IO.FileInfo(OFD.FileName)
    6.             If fInfo.Extension.ToLower = ".txt" Then
    7.                 Using sWrite As New IO.StreamWriter(OFD.FileName, False)
    8.                     '//write here
    9.                 End Using
    10.             End If
    11.         End If
    Last edited by J-Deezy; Sep 22nd, 2010 at 06:49 AM.

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