The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,466 other followers

.net – xcopy ASP.NET / WinForms deployment: find common location to access relative files to it (via: Stack Overflow)

Posted by jpluimers on 2013/12/25

StackOverflow user Joe (sorry, no last name) helped me big time by answering my question on Business logic shared by ASP.NET / WinForms: find the location of the assembly to access relative files to it.

Before showing the code at the bottom of this blog post, let me explain the question in more detail:

Basically I was in the midst of refactoring some ‘inherited’ business logic code that – before refactoring – for the ASP.NET side needs to be initialized with an absolute path, but on the WinForms / WPF side only with a relative path to a GetExecutingAssembly directory.

To ease xcopy deployment, I wanted all configuration settings to be relative. But I hadn’t found a common means for these platforms to obtain a directory usable as a root for accessing relative files.

That way I could put identical settings in both the Web.config and App.config, heck even generate them based on a common fragment, whithout having to hard-code absolute path names.

I knew about Assembly.GetExecutingAssembly, but in ASP.NET that location is not where the web site is (both IIS and the WebDevelopment server make use of temporary locations to store the assemblies).

ASP.NET does have Server.MapPath and HostingEnvironment.MapPath, but I didn’t want to make the business logic depend on ASP.NET.

Joe came up with this solution, which works dandy:

You could use a path relative to AppDomain.CurrentDomain.BaseDirectory.

This will be the directory containing the executable for a client app, and the web site root directory for ASP.NET.

He explained more in his answer to Best way to get application folder path.

The AppDomain.BaseDirectory property of the AppDomain.CurrentDomain gets the right directory, but there are a few things to watch:

Alternatively you can use the AppDomain.RelativeSearchPath property of the AppDomain.CurrentDomain (thanks user superlogical), which returns the bin path in ASP.NET (including trailing Path.DirectorySeparatorChar), but is empty for WinForms.

Hence this little piece of code which uses RelativeSearchPath when not empty, and falls back to BaseDirectory otherwise:

using System;
using BeSharp.IO;

namespace BeSharp.Configuration
    public static class AssemblyHelper
        public static string CurrentDomainBaseDirectory
                string result = AppDomain.CurrentDomain.BaseDirectory;
                result = PathHelper.RemoveTrailingDirectorySeparators(result);
                return result;

        public static string CurrentDomainRelativeSearchPath
                string result = AppDomain.CurrentDomain.RelativeSearchPath;
                return result;

        public static string DeploymentPath
                string result = CurrentDomainRelativeSearchPath;
                if (string.IsNullOrWhiteSpace(result))
                    result = CurrentDomainBaseDirectory;
                return result;


via: .net – Business logic shared by ASP.NET / WinForms: find the location of the assembly to access relative files to it – Stack Overflow.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: