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

查看:156
本文介绍了使用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 )。位置

  • 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?

谢谢,
O

Thanks, O

推荐答案

我将使用GetEntryAssembly()而不是GetExecutingAssembly()。

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

要查看原因,请执行以下操作:

To see why, do this:


  • 创建新的控制台项目

  • 将一个类库项目(ClassLibrary1)添加到解决方案中,并从控制台项目中引用它。

将其放在ClassLibrary1中:

Put this in 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));
        }
    }
}

将其放在控制台的程序中。 cs:

Put this in console's 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:\temp\ClassLibrary1.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:\temp并运行。

Build the solution, copy ClassLibrary1.dll to c:\temp 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天全站免登陆