0

I develop a module working with files. Serving the files in the themes using tilde is OK, but this doesn't seem to work in the controller, where I process the files with System.IO.File methods like Move() or Delete(). For now I use absolute paths, but they are not feasible - the module should be used on different machines and still stored in a single repository.

How to find which root should I use for root-relaive paths in each situation? If there's no universal rule, how to find it while managing upload/download/viewing of files? Or is it more complicated?

My files are stored in: (SolutionRoot)/Orchard.Web/Media/Default/_FooBar/ I've tried all the folders as relative roots and it didn't work.

Pavel V.
  • 2,295
  • 9
  • 36
  • 70

1 Answers1

1

It seems to me that HostingEnvironment.MapPath is probably what you are looking for.

If it isn't, then you can ask the executing assembly for its location and figure things out from there:

var codeBaseUrl = Assembly.GetExecutingAssembly().CodeBase;
var filePathToCodeBase = new Uri(codeBaseUrl).LocalPath;
var directoryPath = Path.GetDirectoryName(filePathToCodeBase);
spender
  • 106,080
  • 28
  • 202
  • 324
  • Likely to be the answer. Now I don't have time to play with it enough, but I'll probably accept it tomorrow. – Pavel V. Dec 02 '15 at 15:26
  • It took me a while to realize how to use it (it's static!), but [HostingEnvironment.MapPath("~")](http://stackoverflow.com/q/275781/1801588) proved to be the solution. Accepted for pointing in the right direction. – Pavel V. Dec 03 '15 at 08:43