Search code examples
c#asp.netiislocalhostfileinfo

file.Exist returns true on localhost and returns false on server c#


I've a grid that contains file list (that on another server) and download buttons for them on each row. When button clicked, file should be downloaded. When I click it on localhost, file.Exist returns true and I can download the file. However, when I try same button on server (IIS), file.Exist returns false and the file cannot be downloaded. (This server also can reach the file.) My code snippet is below;

            var fileNameToShow = "8D.xls";
            var fileNameAndPath = "\\\\10.1.101.151\\Files\\Live\\10\\8D.xls"
            FileInfo file = new FileInfo(fileNameAndPath);
            file.Refresh();
            if (file.Exists)
            {
                // Send the file to the browser
                Response.Clear();
                Response.AddHeader("Content-Disposition",
                    "attachment; filename= " + fileNameToShow + "; size=" + file.Length.ToString());
                Response.TransmitFile(fileNameAndPath);
                Response.Flush();
                Response.End();
            }
            else
            {
                throw new Exception("File does not exist!");
            }

What can I do to solve this?

Edit: File is in 10.1.101.151. IIS is in another server. My local is also another PC.


Solution

  • First, you need to set the permissions of the folder on the server, and add the server where the IIS is located to the server where the 8D.xls is located. enter image description here

    Second, you need to set the identity of the application pool, select a custom account, you can set the administrator.

    enter image description here

    enter image description here