Troy Mitchel
Troy Mitchel

Reputation: 1810

How to verify network files and folders from IIS applications

We have an Intranet web application (ASP.NET 4.5) on SERVER-B. It's on the same domain as SERVER-A. How can I check if a file exists on SERVER-A from the Intranet application? I tried using File.Exits/Directory.Exis, FileWebRequest, cannot get it to work. Is there an IIS setting to allow?

Note: I can browse the files on Server-A from Server-B using file explorer. Note: HttpWebRequest 'does' work for finding files on our external website but that method does not work for our intranet to network.

Method A (result is always false)

File.Exists("\\xyz-123\Shipping\State\ca.doc")
Directory.Exists("\\xyz-123\Shipping\State")

Method B (result is always false and doesn't return an error)

 'url = "file://xyz-123/Shipping/State/ca.doc"
 Private Function FileExist(url As String) As Boolean
    Dim response As WebResponse = Nothing
    Try
        Dim request = DirectCast(WebRequest.Create(url), FileWebRequest)
        request.Method = "HEAD"

        response = DirectCast(request.GetResponse(), WebResponse)
        FileExist = True
    Catch ex As Exception
    Finally
        If response IsNot Nothing Then response.Close()
    End Try

End Function

Upvotes: 0

Views: 251

Answers (2)

PhillipH
PhillipH

Reputation: 6222

Almost certainly your problem is permissions related. Your IIS website runs as an "IIS Application" belonging to an Application Pool in IIS. That Application Pool has a windows login associated with it. That login needs to have permission to acces the files on the second machine.

To fix the problem, look at the application pool setup in IIS and find out what user the application pool is running under, and change the permissions on the file share to allow them access. To debug this you can try assigning your own credentials to the IIS Application Pool your website is running under and you might find it works, becasue you personally do have rights to access the files.

Upvotes: 1

Mahdi
Mahdi

Reputation: 3349

The Method A will work if you share the \\xyz-123\Shipping\State folder with the user running the code.

Upvotes: 1

Related Questions