Results 1 to 3 of 3

Thread: Replacing a File

  1. #1

    Thread Starter
    Hyperactive Member johnweidauer's Avatar
    Join Date
    Sep 2002
    Location
    SLC, UT
    Posts
    314

    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:
    1. Dim Sr As StreamReader
    2.         Dim sFile As String = "g:\" & Request.QueryString("MDN") & ".maildrop"
    3.         Sr = File.OpenText(sFile)
    4.        
    5.         Dim contents As String = Sr.ReadToEnd
    6.        
    7.         MailDropBox.Text = contents
    8.        
    9.         Sr.Close()
    10.         Sr = Nothing

    then i edit the text and I click on the save button.
    VB Code:
    1. Dim sFile As String = "g:\" & Request.QueryString("MDN") & ".maildrop"
    2.         File.Delete(sFile)
    3.         File.Create(sFile)
    4.         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.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Replacing a File

    How about this?
    VB Code:
    1. If IO.File.Exists(sFile) Then
    2.                     Dim success As Boolean = False
    3.                     Do Until success
    4.                         Try
    5.                             IO.File.Delete(sFile)
    6.                             success = True
    7.                         Catch
    8.                             'file is locked, do nothing on failure
    9.                         End Try
    10.                     Loop
    11.                 End If

  3. #3
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    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
  •  



Click Here to Expand Forum to Full Width