使用 Assembly vs AppDomain 查找我的主要可执行文件的路径 [英] Finding my main executable's path using Assembly vs AppDomain

查看:21
本文介绍了使用 Assembly vs AppDomain 查找我的主要可执行文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 .NET 用户,我的目标很简单,就是找到我的主要执行程序集(EXE 文件)目录的绝对路径.

I'm a .NET user, and my goal is as simple as finding the absolute path of the directory of my main executing assembly (the EXE file).

我有几个候选人:

  • Assembly.GetExecutingAssembly().CodeBase
  • Assembly.GetExecutingAssembly().Location
  • AppDomain.CurrentDomain.BaseDirectory

如果根据 .NET 文档来判断 - 我倾向于 CodeBase.谁能用比 .NET 文档更具体的术语来阐明这三个方面?一个例子来证明差异?

If to judge by the .NET documentation - I'm leaning towards CodeBase. Can anyone shed light over all three in a bit more specific terms than the .NET documentation? An example to demonstrate the difference perhaps?

推荐答案

我会使用 GetEntryAssembly() 而不是 GetExecutingAssembly().

I would use GetEntryAssembly() instead of GetExecutingAssembly().

要了解原因,请执行以下操作:

To see why, do this:

  • 创建一个新的控制台项目
  • 将类库项目 (ClassLibrary1) 添加到解决方案并从控制台项目中引用它.
  • Create a new Console Project
  • Add a class library project (ClassLibrary1) to the solution and reference it from the Console Project.

将其放入ClassLibrary1:

namespace ClassLibrary1
{
    using System;
    using System.IO;
    using System.Reflection;

    public class Class1
    {
        public void GetInfo(int n)
        {
            Assembly asm = Assembly.GetEntryAssembly();
            Console.WriteLine("[GetEntryAssembly {0}] Location:{1}", n, Path.GetDirectoryName(asm.Location));
            asm = Assembly.GetExecutingAssembly();
            Console.WriteLine("[GetExecutingAssembly() {0}] Location:{1}", n, Path.GetDirectoryName(asm.Location));
        }
    }
}

将其放入控制台的Program.cs:

namespace ConsoleApplication4
{
    using System;
    using System.IO;
    using System.Reflection;
    using ClassLibrary1;

    class Program
    {
        static void Main(string[] args)
        {
            Assembly asm = Assembly.GetEntryAssembly();
            Console.WriteLine("[GetEntryAssembly() 1] Location:{0}", Path.GetDirectoryName(asm.Location));
            asm = Assembly.GetExecutingAssembly();
            Console.WriteLine("[GetExecutingAssembly() 1] Location:{0}", Path.GetDirectoryName(asm.Location));

            Class1 obj1 = new Class1();
            obj1.GetInfo(2);

            asm = Assembly.LoadFile(@"C:	empClassLibrary1.dll");
            Type t = asm.GetType("ClassLibrary1.Class1");
            object obj2 = asm.CreateInstance("ClassLibrary1.Class1");
            t.GetMethod("GetInfo").Invoke(obj2, new object[] { 3 });

            Console.ReadKey();
        }
    }
}

构建解决方案,将 ClassLibrary1.dll 复制到 c: emp 并运行.

Build the solution, copy ClassLibrary1.dll to c: emp and run.

如您所见,GetExecutingAssembly() 可能会在某些情况下欺骗您.

As you will see, GetExecutingAssembly() may trick you in certain conditions.

最后一点,如果您的应用程序是 Windows Forms 应用程序,您只需使用 Application.ExecutablePath.

One last note, if your app is a Windows Forms one, you can just use Application.ExecutablePath.

这篇关于使用 Assembly vs AppDomain 查找我的主要可执行文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆