locked
file.Exists function not working RRS feed

  • Question

  • User-1458744719 posted

    file.Exists function not working

     Dim f1 As New FileInfo(Server.MapPath("/CaseStudyImages/TemporaryImages/"))
                Dim f2 As New FileInfo(Server.MapPath("/CaseStudyImages/" & success & "/"))
                Dim dir1 As DirectoryInfo = f1.Directory
                Dim dir2 As DirectoryInfo = f2.Directory
                 Dim Folder1Files As FileInfo() = dir1.GetFiles()
                If Folder1Files.Length > 0 Then
                    For Each aFile As FileInfo In Folder1Files
                        If File.Exists("f1" & aFile.Name) Then
                            File.Delete("f2" & aFile.Name)
                        End If
                        aFile.MoveTo("f2" & aFile.Name)
                    Next
            End If
    
    
    
    
    In this line its showing errror, File.Exists("f1" & aFile.Name) Then
    System.UnauthorizedAccessException: Access to the path is denied. 
    i think tprb is with exists function
    Tuesday, February 7, 2012 6:59 AM

Answers

  • User-1458744719 posted

    sorry u were wrong 

    we have to pass string in exist function 

    here is the working code

     Dim filepathName As String = HttpContext.Current.Server.MapPath("/CaseStudyImages/" & success & "/")
                Dim filepathName2 As String = HttpContext.Current.Server.MapPath("/CaseStudyImages/TemporaryImages/")
    
                Dim dir1 As New DirectoryInfo(filepathName2)
                Dim dir2 As New DirectoryInfo(filepathName)
                Dim Folder1Files As FileInfo() = dir1.GetFiles()
                If Folder1Files.Length > 0 Then
                    For Each aFile As FileInfo In Folder1Files
                        aFile.MoveTo(filepathName & aFile.Name)
                        If File.Exists(filepathName & aFile.Name) Then
                            File.Delete(filepathName2 & aFile.Name)
                        End If
                    Next
                End If
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 7, 2012 7:33 AM

All replies

  • User992646781 posted

    Which part of "Access to the path is denied" you do not understand? File.Exists function requires read permissions on the path that's being checked, you need to sort out permissions.

    Tuesday, February 7, 2012 7:08 AM
  • User-1458744719 posted

    sorry u were wrong 

    we have to pass string in exist function 

    here is the working code

     Dim filepathName As String = HttpContext.Current.Server.MapPath("/CaseStudyImages/" & success & "/")
                Dim filepathName2 As String = HttpContext.Current.Server.MapPath("/CaseStudyImages/TemporaryImages/")
    
                Dim dir1 As New DirectoryInfo(filepathName2)
                Dim dir2 As New DirectoryInfo(filepathName)
                Dim Folder1Files As FileInfo() = dir1.GetFiles()
                If Folder1Files.Length > 0 Then
                    For Each aFile As FileInfo In Folder1Files
                        aFile.MoveTo(filepathName & aFile.Name)
                        If File.Exists(filepathName & aFile.Name) Then
                            File.Delete(filepathName2 & aFile.Name)
                        End If
                    Next
                End If
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 7, 2012 7:33 AM