Getting the calling application path

Hi,

Is there anyway of finding the application name from a class I'm trying to build a configuration setting class (like the one in the full framework). It all works fine if it is called from the application .exe but if I call it from a class which has been created by the exe it does not work. The reason being that I get the path of the calling assembly and then add .Config to it get the settings file eg

assemblyPath = Assembly.GetCallingAssembly().GetName().CodeBase;
configFileURL = assemblyPath + FILE_EXT;

Is there any other way of getting the application filename, either through a specific method or by walking up the call stack.

Thanks in Advance.

Andy Hough 



Answer this question

Getting the calling application path

  • Dottj

    Andy,

    This should give you what you want:

    
        public string GetLoadingAssembly()
        {
          System.Reflection.Assembly assembly = System.Reflection.Assembly.GetCallingAssembly();
          System.Reflection.AssemblyName assemblyName = assembly.GetName();
          return assemblyName.CodeBase;
        }
    
    

    HTH
    Dan


  • .net sukbir

    Andy,

    My understanding is that you want to get the path of the original executable that loads a class whether or not that class is in the executables assembly. For instance, given

    Assembly A
    ==============
    public class AA
    {
    public static void Main()
    {
    BB bb = new BB();
    }


    public string LoadingAssembly
    {
    get { ... }
    }
    }

    public class AB
    {
    public AB()
    {
    }

    public string LoadingAssembly
    {
    get { ... }
    }
    }


    Assembly B
    ==============
    public class BB
    {
    public BB()
    {
    }

    public string LoadingAssembly
    {
    get { ... }
    }
    }

    then, AA.LoadingAssembly, AB.LoadingAssembly, and BB.LoadingAssembly should return the path to assembly A. Is that correct

    Dan


  • dreameR.78

    Thats right Dan, I want to find the path base executable but I will not know the hierarchy of classes at run time.
  • Getting the calling application path