getting "System.IO.IOException: The process cannot access the file" while delete a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • smanisankar@gmail.com

    #1

    getting "System.IO.IOException: The process cannot access the file" while delete a file

    hi,
    the following is the full page code for uploading a file to server.
    since i got no idea to overwrite the file, i want delete the file if
    the file is already uploaded.
    i got the folder name and filename of the file to delete from the
    request.QuerySt ring("path")
    so i got the above error when i try to delete the file before upload.
    Please anyone help me to solve out from this error.

    <%@ Import Namespace="Syst em.IO" %>
    <%@ Page Language="vb" %>
    <html>
    <Script Language="VB" RunAt="Server">
    Dim MyPath, MyName as string

    Sub Page_Load(Sende r as Object, e as EventArgs)
    dim serpath as string
    MyPath = request.QuerySt ring("path")
    MyPath = replace(MyPath, "/","\")
    MyPath = replace(MyPath, """","")
    serpath = server.MapPath( "") ' Set the path.
    MyPath = serpath & "\" & mypath
    End Sub

    Sub Upload_Click(Se nder as Object, e as EventArgs)
    ' Display properties of the uploaded file
    if File.exists(myp ath) then
    File.Delete(myp ath)
    end if

    ' Let us recover only the file name from its fully qualified path at
    client
    Dim strFileName as string
    strFileName = MyFile.PostedFi le.FileName

    ' Let us Save uploaded file to server at
    C:\ServerFolder \
    Try
    MyFile.PostedFi le.SaveAs(MyPat h)
    catch Exp as exception
    response.Write( "Error Occored")
    response.End()
    End Try
    myfile = nothing
    End Sub
    </Script>
    <Body>
    <table cellpadding="0" cellspacing="0" width="400" align="center">
    <tr>
    <td>
    <Font Color="DarkGree n" Face=Helvetica Size=3> <B>Upload your file
    to replace the old file
    </Font>
    <HR Size="2" Color=Black >
    <P>
    <Form Method="Post" EncType="Multip art/Form-Data" RunAt="Server">
    Choose Your File To Upload : <BR>
    <Input ID="MyFile" Type="File" RunAt="Server" Size="40"> <BR>
    <BR>
    <Input Type="Submit" Value="Upload"
    OnServerclick=" Upload_Click" RunAt="Server">
    <P>
    </Form>
    <HR Size="2" Color=Black>
    </td>
    </tr>
    </table>
    </Body>
    </html>



    Thanks,
    Mani

Working...