1

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...

Clint Davis
  • 441
  • 1
  • 12
  • 28
  • Do you want people to be able to get to the file directly via a URL? If not, you probably want to put it in App_Data instead of the root of your website. – tvanfosson Jul 16 '10 at 14:31
  • I don't really care if they get this file, it's a test project right now. But thanks for the heads up because this will be production eventually. – Clint Davis Jul 16 '10 at 14:41

5 Answers5

7
GetWorkBook(Server.MapPath("tplBud806_wRevenue.xls"));
Simon Hazelton
  • 1,235
  • 10
  • 13
  • And for the explanation, see the answer by John Weldon. – Hans Kesting Jul 16 '10 at 14:30
  • For MVC users that came across this solution, check this question for another method: http://stackoverflow.com/questions/4616646/specifying-relative-file-location-in-web-config-for-use-by-standard-c-sharp-clas – mao47 Jan 09 '14 at 18:39
3

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
  • 37,037
  • 10
  • 91
  • 126
1

Server.MapPath?

Jeremy
  • 4,625
  • 2
  • 19
  • 24
1

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
  • 62,163
  • 14
  • 99
  • 133
1

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

Community
  • 1
  • 1
Philippe
  • 1,663
  • 1
  • 13
  • 25