Results 1 to 14 of 14

Thread: [RESOLVED] deleting a file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Resolved [RESOLVED] deleting a file

    hey guys. If the user chooses to save over an existing file, I want to essentially delete the file first, then re-create it and save to it. Basically, I don't want to append to the file. I want to completely overwrite. However, I am just running into all these snags. It basially deletes the file as soon as the savefiledialog box opens. before, it would simply append. Can anyone spot my problem?
    code Code:
    1. Try
    2.             With SaveDataLogDialog
    3.                 .FileName = DFileName
    4.                 If File.Exists(DFileName) = True Then
    5.                     File.Delete(DFileName)
    6.                 End If
    7.  
    8.                
    9.                 .Filter = "CSV files (*.csv)|*.csv|" & "All files|*.*"
    10.                 .DefaultExt = "csv"
    11.                 .AddExtension = True
    12.                 .CreatePrompt = True
    13.                 .OverwritePrompt = True
    14.                 .Title = "Save Data Log As..."
    15.  
    16.            
    17.                 If .ShowDialog() = DialogResult.OK Then
    18.                  
    19.                     DFileName = .FileName
    20.  
    21.                 End If
    22.  
    23.             End With
    24.         Catch es As Exception
    25.             MessageBox.Show(es.Message)
    26.         Finally
    27.             If Not (sw Is Nothing) Then
    28.                 datalogSW.Close()
    29.             End If
    30.         End Try

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: deleting a file

    Hey,

    Try putting this code:

    Code:
         .FileName = DFileName
                    If File.Exists(DFileName) = True Then
                        File.Delete(DFileName)
                    End If
    Here:

    Code:
    If .ShowDialog() = DialogResult.OK Then
         .FileName = DFileName
                    If File.Exists(DFileName) = True Then
                        File.Delete(DFileName)
                    End If                 
    
                        DFileName = .FileName
    
                    End If
    Gary

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Re: deleting a file

    that was originally what I had, but it just appends onto the end of the file. I just your method again to make sure and it still functions the same way

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: deleting a file

    Have you tried stepping through your code in the debugger?

    If the file isn't getting deleted, then it is because this if statement:

    Code:
    If File.Exists(DFileName) = True Then
    If not evaluating to true, and the only way that would happen is if the file doesn't already exist. What is the value of DFileName at runtime. Is the file there?

    Gary

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Re: deleting a file

    i see...somehow, DFilename isn't being created when i press ok. It skips right over the delete. But I don't understand because I am picking a file that exists (it opens before runtime), and I get the overwrite prompt telling me the file already exists...hmmmmm...How do I see what values are held by variables with the debugger?

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: deleting a file

    Once you hit a break point, either:

    1) hover your mouse over the variable in question
    2) Open the Locals Window
    3) Drag the variable into the Watch Window

    Gary

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Re: deleting a file

    DFileName stays completely empty the entire time.. hm. the only other time I use DfileName is in a streamwriter that writes data on ever timer_tick in a seperate sub...

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: deleting a file

    Without seeing the rest of your code, it is going to be difficult to suggest anything.

    If would search your code for:

    Code:
    DFileName =
    Find out all the places that that variable is being used. It might be the case that you need to do this step first:

    Code:
    DFileName = .FileName
    Then delete, but really you have to say exactly what you want your application to do.

    Gary

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: deleting a file

    There's no need to delete the file first, by the way. You can always set its length to zero:

    Code:
    Dim fs As New IO.FileStream("filename.ext", IO.FileMode.OpenOrCreate, IO.FileAccess.Write, IO.FileShare.None)
    If fs.Length > 0 Then fs.SetLength(0)
    
    ' Write your data here

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Re: deleting a file

    Ok, i've tried every method above...but DFileName still stays "Nothing" throughout the test. It never receives a value. Here is all the pertinent code...I tried putting DFileName = .Filename in the FileOK event raised by the savefiledialog, but that didn't work either...I am not good at this stuff so thanks for sticking with me so far.

    code Code:
    1. Private DFileName As String
    2. Private DataText1 As String
    3. Private DataText2 As String
    4. Private SFileName As String
    5. Private datalogSW As System.IO.StreamWriter
    6.  
    7. Try
    8. With SaveDataLogDialog
    9.  
    10.  
    11.  
    12. .Filter = "CSV files (*.csv)|*.csv|" & "All files|*.*"
    13. .DefaultExt = "csv"
    14. .AddExtension = True
    15. .CreatePrompt = True
    16. .OverwritePrompt = True
    17. .Title = "Save Data Log As..."
    18. .CheckPathExists = True
    19.  
    20. If .ShowDialog() = DialogResult.OK Then
    21. .FileName = DFileName
    22. If File.Exists(DFileName) = True Then
    23. File.Delete(DFileName)
    24. End If
    25.  
    26.  
    27. End If
    28.  
    29. End With
    30. Catch es As Exception
    31. MessageBox.Show(es.Message)
    32. Finally
    33. If Not (sw Is Nothing) Then
    34. datalogSW.Close()
    35. End If
    36. End Try
    37.  
    38. End Sub
    39.  
    40. Private Sub SaveTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveTimer.Tick
    41.  
    42. 'Dim fs As New IO.FileStream(DFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Write, IO.FileShare.None)
    43. 'If fs.Length > 0 Then fs.SetLength(0)
    44.  
    45. datalogSW = My.Computer.FileSystem.OpenTextFileWriter(DFileName, True)
    46. DataText1 = CurrentPositionValue.Text
    47. DataText2 = CurrentForceValue.Text
    48. datalogSW.WriteLine(DataText1 & "," & DataText2)
    49. datalogSW.Close()
    50.  
    51. End Sub

  11. #11
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: deleting a file

    you got this line wrong

    If .ShowDialog() = DialogResult.OK Then
    .FileName = DFileName

    should be inverted

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Re: deleting a file

    nevermind. got it. should have been

    DFileName = .Filename

    instead of the other way around...duhhh.....thanks for your help guys

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] deleting a file

    Doh!! I think I saw that, but just never said anything, as I thought you were setting it elsewhere as well.

    Glad you got it sorted!

    Gary

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Re: [RESOLVED] deleting a file

    haha. you did say it in post #8 now that I look back. I don't know why I didn't try it sooner. I think I just figured that's what I did because the way I had it made absolutely no sense. It's alright though--everyone got rep! haha

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