|
-
Nov 2nd, 2006, 02:56 PM
#1
Thread Starter
Hyperactive Member
Replacing a File
I am trying to delete a file on the server, here is my code.
I first read the text from the file and put it into a text box
VB Code:
Dim Sr As StreamReader
Dim sFile As String = "g:\" & Request.QueryString("MDN") & ".maildrop"
Sr = File.OpenText(sFile)
Dim contents As String = Sr.ReadToEnd
MailDropBox.Text = contents
Sr.Close()
Sr = Nothing
then i edit the text and I click on the save button.
VB Code:
Dim sFile As String = "g:\" & Request.QueryString("MDN") & ".maildrop"
File.Delete(sFile)
File.Create(sFile)
File.AppendAllText(sFile, MailDropBox.Text)
this process gives me this error on line [File.Delete(sFile)]:
The process cannot access the file 'g:\filename.maildrop' because it is being used by another process.
maybe there is a way to overwrite the text in the file, but i am not sure, I will read up more on the objects that I use.
Any help would be appreciated.
Thanks
To the world you may just be one person, but to this one person, you just might be the world.
-
Nov 2nd, 2006, 04:35 PM
#2
Re: Replacing a File
How about this?
VB Code:
If IO.File.Exists(sFile) Then
Dim success As Boolean = False
Do Until success
Try
IO.File.Delete(sFile)
success = True
Catch
'file is locked, do nothing on failure
End Try
Loop
End If
-
Nov 4th, 2006, 02:25 AM
#3
Frenzied Member
Re: Replacing a File
wild bill is right.
always use File or FileInfo objects.
if i remember correctly, you also can pass a parameter when saving to force overwriting...check it out yourself...dont have vs on this machine now
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
|