locked
File exist not working RRS feed

  • Question

  • User66371569 posted

    Hi  I have  written code to check if file exist in folder or no   but always code goes to else 

    Dim sourcefile = Request.ApplicationPath & "/Portal-Photos/" & Session("EMPNO") & ".jpg"

    If System.IO.File.Exists(sourcefile) = True Then

     MsgBox("file is exist")

    Else

     MsgBox("file not exist")

    End If

    Monday, June 15, 2020 7:18 AM

All replies

  • User-719153870 posted

    Hi thepast,

    For File. Exists(String) Method, the parameter should be physical not virtual one. Please modify your code like below:

    If System.IO.File.Exists(Server.MapPath(sourcefile)) = True Then

    Best Regard,

    Yang Shen

    Monday, June 15, 2020 7:57 AM
  • User753101303 posted

    Hi,

    Double check what you have in sourceFile. I would expect a virtual path (is something  such as to your file ie something  such as /yoursite.com/portal-photos/123.jpg) rather than a physical path and so it tries to check for a yoursite.com folder on the current drive.

    Compare with what you have using :

    sourcefile=Server.MapPath("~/Portal-Photos/" & Session("EMPNO") & ".jpg") which shoud give something such as c:\inetpub\yoursite\portal-photos\123.jpg

    Monday, June 15, 2020 8:02 AM
  • User66371569 posted

    I modified 

    If System.IO.File.Exists(Server.MapPath(sourcefile)) = True Then

    still same jump to else whether file exist or no

    Monday, June 15, 2020 8:29 AM
  • User753101303 posted

    One step at a time. Please do look at what is returned by Server.MapPath(sourcefile) to see if this is the correct location. For example if your application is under a web site root using ~ does make a difference. So I would really try :

    Dim sourcefile = Server.MapPath("~/Portal-Photos/" & Session("EMPNO") & ".jpg")
    ' You can debug here if sourcefile contains the expected file path
    If System.IO.File.Exists(sourcefile) = True Then
     MsgBox("file is exist")
    Else
     MsgBox("file not exist")
    End If

    Not directly related but :
    - if System.IO.File.Exists(sourcefile) Then is enough
    - MsgBox can help in local debugging but shouldn't be used on the actual app (it shows a message box on the server side)

    Monday, June 15, 2020 8:55 AM