views:

907

answers:

1

I have an assmebly that will be used in both a desktop app and an asp.net website.

I need to deal with relative paths (local files, not urls) in either situation.

How can i implement this method?

string ResolvePath(string path);

Under a web envronment, id expect the method to behave like this (where d:\wwwroot\mywebsite is the folder iis points at):

/folder/file.ext => d:\wwwroot\mywebsite\folder\file.ext
~/folder/file.ext => d:\wwwroot\mywebsite\folder\file.ext
d:\wwwroot\mywebsite\folder\file.ext => d:\wwwroot\mywebsite\folder\file.ext

for a desktop environment: (where c:\program files\myprogram\bin\ is the path of the .exe)

/folder/file.ext => c:\program files\myprogram\bin\folder\file.ext
c:\program files\myprogram\bin\folder\file.ext => c:\program files\myprogram\bin\folder\file.ext

I'd rather not inject a different IPathResolver depending on what state its running in.

How do I detect which environment I'm in, and then what do i need to do in each case to resolve the possibly-relative path?

Thanks

+1  A: 

As mentioned in John's comment, relative to what? You can use System.IO.Path.Combine method to combine a base path with a relative path like:

 System.IO.Path.Combine(Environment.CurrentDirectory, relativePath);

You can replace Environment.CurrentDirectory in the above line with whatever base path you want.

You could store the base path in the configuration file.

Mehrdad Afshari