1

I have an ASP.NET project I'm currently working on. There is a C# file (dbedit.cs) that is used to directly access and edit 2 XML database files. How can I create a relative path of the XML files (both in the same directory) to dbedit.cs? It needs to be portable so it can't be hard-coded in. dbedit.cs is also accessed from two other projects that are in the same solution, so the assembly path of dbedit.cs is different depending on which project is accessing it.

This doesn't work for that reason:

(new System.Uri(Path.GetDirectoryName(Assembly.GetAssembly(typeof(dbedit)).CodeBase))).LocalPath;

Any help would be greatly appreciated.

1 Answer 1

2

You can use something like HttpContext.Current.Server.MapPath. This needs to be a web-project though.

[Edit] Here are documentation for MapPath: http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't work because dbedit.cs is a regular C# file, so there is no HttpContext that can be called on it. It gets accessed by several aspx pages, as well as a windows form that is used as a GUI front-end to edit the XML files.
You access it by a gui too, well then theres a problem. Because when it is accessed from a aspx, even if it is a regular C# file HttpContext is still availible. I would probably solve this by checking if it's a web-app, then call the code above, else get the assembly working directory than concatinate it with the relative path (google it). Something like if(HttpContext.Current == null) return Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(MyClass)).CodeBase), relativePath); else return HttpContext.Current.Server.MapPath(relativePath);. Haven't tried this though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.