Free Support Forum - aspose.com

Saving file: access denied to path

I try to open an Excel, change some data and saving it again with the same file name and on the same location (meaning: overwriting). At the point where the files is saved I receive the error: "Access to the path “blabla” is denied."

My code:
Excel excel = new Excel();
excel.Open(Server.MapPath(dirName) + “\” + fileName);
excel.Worksheets.GetRangeByName(“TEST”)[0,0].PutValue(DateTime.Now.Date);
excel.Save(Server.MapPath(dirName) + “\” + fileName, FileFormatType.Default);

If I change the file name to something else, the saving works (even when the other file name already exists, the file is overwritten).

Why can’t I save and overwtite the file I opened in the same procedure?

It doesn't seem a problem of Aspose.Excel. It seems you didn't assign enough rights to write file on your server.

Following is my sample code and it works fine:

static void Main(string[] args)

{

Excel excel = new Excel();
excel.Open("d:\\test\\book2.xls");
excel.Worksheets.GetRangeByName("Test")[0,0].PutValue(DateTime.Now.Date);
excel.Save("d:\\test\\book2.xls", FileFormatType.Default);
}

Do you mean if you change your code to something like this:

Excel excel = new Excel();
excel.Open(Server.MapPath(dirName) + "\\" + fileName);
excel.Worksheets.GetRangeByName("TEST")[0,0].PutValue(DateTime.Now.Date);
excel.Save(Server.MapPath(dirName) + "\\" + "book1.xls", FileFormatType.Default);

Then all work fine in your machine?

Indeed, if I specify another file name than the original file I have no problems saving the file. Even when that other file name already exists, the file is overwritten.

I just can’t seem te save the original file.

I gave the IUSR and ASPNET account full control to the directory, but with no luck.

Really strange. What happens if you write a console application with same code?

Strange indeed. I made a console application with the same code and it worked perfectly.

So I figured out it had to be a problem with access rights. And it did, I found the problem. The source file I used I received from my customer, I just copied it to the location I used. And it seemed the file did not get all the correct rights from the folder I placed it in. The ASPNET did not have full control on the file. After giving it that rights, it worked.

Thanks for all the help.