views:

51

answers:

5

This should be pretty simple but it's not working.

I have a file underneath the root of my project. I want to call it like this.

GetWorkbook("tplBud806_wRevenue.xls")

I publish the project out to the server and try to run it and the server says it can't find it.

Could not find file 'c:\windows\system32\inetsrv\tplBud806_wRevenue.xls'.

That's not the path it should be taking. It should be under E:\IIServer\rootwww\reports\tplBud806_wRevenue.xls.

I thought relative paths were supposed to start from the path that the project was running in. I've also tried.

GetWorkbook("/tplBud806_wRevenue.xls")
GetWorkbook("\tplBud806_wRevenue.xls")
GetWorkbook("~/tplBud806_wRevenue.xls")
GetWorkbook("~\tplBud806_wRevenue.xls")

Is there some setting I'm missing? It's got to be something simple...

+6  A: 
GetWorkBook(Server.MapPath("tplBud806_wRevenue.xls"));
Simon Hazelton
And for the explanation, see the answer by John Weldon.
Hans Kesting
+1  A: 

Server.MapPath?

Jeremy
+3  A: 

GetWorkbook is not an ASP.NET function, and it likely defaults to the folder that the process calling it was started from. The process in this case is an IIS process and probably started in that folder.

John Weldon
Thanks for the explanation. I'm not as familiar with ASP.net so this is new to me.
Clint Davis
You're welcome :)
John Weldon
+1  A: 

Your application is running in an AppDomain loaded by the w3wp.exe located in the directory in your error. Which means that trying to look for any file will start in that directory. You should use Page.MapPath, as mentioned by others. It tells the application to start looking in the folder your aspx is in.

Yuriy Faktorovich
+1  A: 

GetWorkBook(Server.MapPath("~/tplBud806_wRevenue.xls")); If the .XLS file is at the root of your project.

You can use also use ~ in conjuction with ResolveURL() to access an URL in your site. So ~ will be replaced by the root URL of your project

Example:

ResolveURL("~\tplBud806_wRevenue.xls")

will be transformed to http://myproject.url/website/tplBud806_wRevenue.xls

If you need disk access, like in your example, use Server.MapPath

Look at this SO post to learn more about Server.MapPath

Philippe