How do I get the path of the assembly the code is in?

前端 未结 30 2794
小蘑菇
小蘑菇 2020-11-21 16:17

Is there a way to get the path for the assembly in which the current code resides? I do not want the path of the calling assembly, just the one containing the code.

<
相关标签:
30条回答
  • 2020-11-21 16:56

    The current directory where you exist.

    Environment.CurrentDirectory;  // This is the current directory of your application
    

    If you copy the .xml file out with build you should find it.

    or

    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(SomeObject));
    
    // The location of the Assembly
    assembly.Location;
    
    0 讨论(0)
  • 2020-11-21 16:56

    I use this to get the path to the Bin Directory:

    var i = Environment.CurrentDirectory.LastIndexOf(@"\");
    var path = Environment.CurrentDirectory.Substring(0,i); 
    

    You get this result:

    "c:\users\ricooley\documents\visual studio 2010\Projects\Windows_Test_Project\Windows_Test_Project\bin"

    0 讨论(0)
  • 2020-11-21 16:57

    I've been using Assembly.CodeBase instead of Location:

    Assembly a;
    a = Assembly.GetAssembly(typeof(DaoTests));
    string s = a.CodeBase.ToUpper(); // file:///c:/path/name.dll
    Assert.AreEqual(true, s.StartsWith("FILE://"), "CodeBase is " + s);
    s = s.Substring(7, s.LastIndexOf('/') - 7); // 7 = "file://"
    while (s.StartsWith("/")) {
        s = s.Substring(1, s.Length - 1);
    }
    s = s.Replace("/", "\\");
    

    It's been working, but I'm no longer sure it is 100% correct. The page at http://blogs.msdn.com/suzcook/archive/2003/06/26/assembly-codebase-vs-assembly-location.aspx says:

    "The CodeBase is a URL to the place where the file was found, while the Location is the path where it was actually loaded. For example, if the assembly was downloaded from the internet, its CodeBase may start with "http://", but its Location may start with "C:\". If the file was shadow-copied, the Location would be the path to the copy of the file in the shadow copy dir. It’s also good to know that the CodeBase is not guaranteed to be set for assemblies in the GAC. Location will always be set for assemblies loaded from disk, however."

    You may want to use CodeBase instead of Location.

    0 讨论(0)
  • 2020-11-21 16:58

    In all these years, nobody has actually mentioned this one. A trick I learned from the awesome ApprovalTests project. The trick is that you use the debugging information in the assembly to find the original directory.

    This will not work in RELEASE mode, nor with optimizations enabled, nor on a machine different from the one it was compiled on.

    But this will get you paths that are relative to the location of the source code file you call it from

    public static class PathUtilities
    {
        public static string GetAdjacentFile(string relativePath)
        {
            return GetDirectoryForCaller(1) + relativePath;
        }
        public static string GetDirectoryForCaller()
        {
            return GetDirectoryForCaller(1);
        }
    
    
        public static string GetDirectoryForCaller(int callerStackDepth)
        {
            var stackFrame = new StackTrace(true).GetFrame(callerStackDepth + 1);
            return GetDirectoryForStackFrame(stackFrame);
        }
    
        public static string GetDirectoryForStackFrame(StackFrame stackFrame)
        {
            return new FileInfo(stackFrame.GetFileName()).Directory.FullName + Path.DirectorySeparatorChar;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 16:58

    in a windows form app, you can simply use Application.StartupPath

    but for DLLs and console apps the code is much harder to remember...

    string slash = Path.DirectorySeparatorChar.ToString();
    string root = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    
    root += slash;
    string settingsIni = root + "settings.ini"
    
    0 讨论(0)
  • 2020-11-21 16:59

    For ASP.Net, it doesn't work. I found a better covered solution at Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?. It works for both Win Application and ASP.Net Web Application.

    public string ApplicationPath
        {
            get
            {
                if (String.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath))
                {
                    return AppDomain.CurrentDomain.BaseDirectory; //exe folder for WinForms, Consoles, Windows Services
                }
                else
                {
                    return AppDomain.CurrentDomain.RelativeSearchPath; //bin folder for Web Apps 
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题