locked
file not deleteing RRS feed

  • Question

  • User-125499312 posted

    i trying to delete a file on my server, but the file is still there after this runs

    here is my code:

    dim strInvitationInsideGenericFileName as string = "C:\Inetpub\wwwroot\MyLabels\EmailAttachments\Invitation_Outside_EventID_182.pdf"

    If System.IO.File.Exists(strInvitationInsideGenericFileName) = True Then
        System.IO.File.Delete(strInvitationInsideGenericFileName)
    End If

    thx for ur help

    Monday, August 31, 2020 5:52 PM

All replies

  • User753101303 posted

    Hi,

    Basic debugging. I would except Delete to throw an exception if the file can"t be deleted. If you dont hide exceptions it could mean that for some reason System.IO.File.Exists(strInvitationInsideGenericFileName) really returns false (maybe because of an hard to spot difference such as maybe having __ rather than _ in the actual file?. It could be also that this file is deleted and then created again there later.

    In short even for a couple of lines, you should rather use the debugger to see what your code does exactly and then fix that rather than wondering what could happen from your code. As you see you can often find multiple possible problems nbut then you have no way to know which one actually happens.

    Monday, August 31, 2020 6:05 PM
  • User1535942433 posted

    Hi yzidell,

    Accroding to your codes,as far as I think,you could check follow this:

    1.Make sure the ASP user has permissions to this folder. By default this user is not given access to much of the harddrive.

    2.Make sure the file's path is right.

    I suggest you could debug and breakpint to check wheather your codes are right and have no errors.

    This is a demo:

    Public Sub DeleteFileFromFolder(ByVal StrFilename As String)
        Dim strPhysicalFolder As String = Server.MapPath("..\")
        Dim strFileFullPath As String = strPhysicalFolder & StrFilename
    
        If IO.File.Exists(strFileFullPath) Then
            IO.File.Delete(strFileFullPath)
        End If
    End Sub

    If you have errors and can't solve it by yourself,you could post your errors and full codes to us.It will help us to solve your problems.

    Best regards,

    Yijing Sun

    Tuesday, September 1, 2020 2:48 AM