1

I have a functionality wherein I write an excel file in a folder in application's solution and read it from there and allow save . It is working fine in local but after deploying in the server it is unable to write and this throws error on read operation saying that the file could not be found.

Please refer to the code below.

 Microsoft.Office.Interop.Excel.Workbook workbook;
            Microsoft.Office.Interop.Excel.Worksheet NwSheet;

            appExl = new Microsoft.Office.Interop.Excel.Application();
            string serverPath = Server.MapPath(".");

            string filenameToLoad = serverPath + "\\Page1Reports\\" + Session["UserAccentureID"].ToString() + ".xls";


            FileStream fileStream = new FileStream(filenameToLoad, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
            fileStream.Write(result, 0, result.Length);
            fileStream.Close();



//some more manipulation

            workbook.Save();
            workbook.Close();
            appExl.Quit();



            System.IO.FileInfo file = new System.IO.FileInfo(filenameToLoad);

            if (file.Exists)
            {
                Response.Clear();
                Response.ClearHeaders();
                Response.Charset = "";
                Response.ContentType = "Application/vnd.xls";
                Response.AddHeader("content-disposition", "attachment;filename=Page1SLScoreCardReport.xls");
                Response.AddHeader("Cache-Control", "max-age=0");
                Response.WriteFile(file.FullName);

            }

Please let me know what could be the issue?

Richa
  • 397
  • 1
  • 9
  • 22
  • Does your IIS user have appropriate rights for the directory and the file? – Ondrej Peterka May 31 '13 at 05:59
  • I went to Properties->Security-> Rights were there for ADMIn and USERS had the rights..although CREATOR OWNER did not have all rights and was not allowing updations as well. I also removed read only to the folder. Please suggest now . Do I have to check elsewhere? – Richa May 31 '13 at 06:25
  • What error exactly you are getting ..and at which line ?? – Pranav May 31 '13 at 07:32
  • Could not find file 'G:\TestEnvironment\ALN_TestSite_ForDeveloper\Page1Reports\10697696.xls'. meaning the file is not written so when it tries to read it it is unable to do so. – Richa May 31 '13 at 08:35

1 Answers1

0

Check this SO question: User ASP.NET runs under . It shows you (based on your IIS version), which user it is run under. Most likely you will have to give rights fro the folder/file to IIS_IUSRS group (if you have IIS 7SP2 or higher). (Using Properties/Security dialog).

Also double check the path ... sometimes the dumbest mistake is the one which causes the problem ;)

UPDATE 1: Also make sure you ar eusing the MapPath correctly. You are using .. Maybe ~ is what you want. Here is great summary of differences: Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

enter image description here

Community
  • 1
  • 1
Ondrej Peterka
  • 2,964
  • 4
  • 31
  • 48
  • I logged the path that is generated on server and that is correct.:( – Richa May 31 '13 at 08:29
  • Hi I am so sorry..the whole problem was in code- FileStream fileStream = new FileStream(filenameToLoad, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); File Mode was open..so it was replacing file already existing in case of no file it was failing to write. I relaced the mode with create and working fine. But thanks for all the support. :) – Richa May 31 '13 at 10:14
  • @Richa Thanks for letting me know. Glad to hear you find the solution! Happy coding ;) – Ondrej Peterka May 31 '13 at 10:52