|
-
Jul 20th, 2010, 03:07 PM
#1
Thread Starter
Lively Member
[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:
Try
With SaveDataLogDialog
.FileName = DFileName
If File.Exists(DFileName) = True Then
File.Delete(DFileName)
End If
.Filter = "CSV files (*.csv)|*.csv|" & "All files|*.*"
.DefaultExt = "csv"
.AddExtension = True
.CreatePrompt = True
.OverwritePrompt = True
.Title = "Save Data Log As..."
If .ShowDialog() = DialogResult.OK Then
DFileName = .FileName
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
Finally
If Not (sw Is Nothing) Then
datalogSW.Close()
End If
End Try
-
Jul 20th, 2010, 03:17 PM
#2
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
-
Jul 20th, 2010, 03:22 PM
#3
Thread Starter
Lively Member
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
-
Jul 20th, 2010, 03:24 PM
#4
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
-
Jul 20th, 2010, 03:30 PM
#5
Thread Starter
Lively Member
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?
-
Jul 20th, 2010, 03:33 PM
#6
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
-
Jul 20th, 2010, 03:47 PM
#7
Thread Starter
Lively Member
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...
-
Jul 21st, 2010, 12:56 AM
#8
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:
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
-
Jul 21st, 2010, 01:04 AM
#9
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
-
Jul 21st, 2010, 07:24 AM
#10
Thread Starter
Lively Member
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:
Private DFileName As String
Private DataText1 As String
Private DataText2 As String
Private SFileName As String
Private datalogSW As System.IO.StreamWriter
Try
With SaveDataLogDialog
.Filter = "CSV files (*.csv)|*.csv|" & "All files|*.*"
.DefaultExt = "csv"
.AddExtension = True
.CreatePrompt = True
.OverwritePrompt = True
.Title = "Save Data Log As..."
.CheckPathExists = True
If .ShowDialog() = DialogResult.OK Then
.FileName = DFileName
If File.Exists(DFileName) = True Then
File.Delete(DFileName)
End If
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
Finally
If Not (sw Is Nothing) Then
datalogSW.Close()
End If
End Try
End Sub
Private Sub SaveTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveTimer.Tick
'Dim fs As New IO.FileStream(DFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Write, IO.FileShare.None)
'If fs.Length > 0 Then fs.SetLength(0)
datalogSW = My.Computer.FileSystem.OpenTextFileWriter(DFileName, True)
DataText1 = CurrentPositionValue.Text
DataText2 = CurrentForceValue.Text
datalogSW.WriteLine(DataText1 & "," & DataText2)
datalogSW.Close()
End Sub
-
Jul 21st, 2010, 07:29 AM
#11
Frenzied Member
Re: deleting a file
you got this line wrong
If .ShowDialog() = DialogResult.OK Then
.FileName = DFileName
should be inverted
-
Jul 21st, 2010, 07:30 AM
#12
Thread Starter
Lively Member
Re: deleting a file
nevermind. got it. should have been
DFileName = .Filename
instead of the other way around...duhhh.....thanks for your help guys
-
Jul 21st, 2010, 07:42 AM
#13
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
-
Jul 21st, 2010, 07:47 AM
#14
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|