这是this answer(及其评论)的后续行动。从程序集到进程获取可执行文件名称与区别?从程序集vs进程获取当前可执行文件的名称?
System.Reflection.Assembly.GetCallingAssembly.GetEntryAssembly().CodeBase
VS
Process.GetCurrentProcess().ProcessName
我假定这将是相同的所有的时间?没有?有没有优点和缺点?
这是this answer(及其评论)的后续行动。从程序集到进程获取可执行文件名称与区别?从程序集vs进程获取当前可执行文件的名称?
System.Reflection.Assembly.GetCallingAssembly.GetEntryAssembly().CodeBase
VS
Process.GetCurrentProcess().ProcessName
我假定这将是相同的所有的时间?没有?有没有优点和缺点?
ProcessName是操作系统主机进程名称。
Assembly CodeBase指向给定进程中的程序集。相同的程序集可以由不同的进程托管。
不,他们不需要返回相同的值。
碰巧,我就遇到了这个“疑难杂症”最近:他们可以返回取决于是否你直接运行.exe文件不同的值,或从MSVS调试器的内部:
How do I get the .exe name of a C# console application?
这只是一个例子 - 我确定可能会有其他人。
'希望有帮助!
它们不一定相同。编译这两个程序在同一目录下的控制台应用程序:
// In Test.cs, compile to Test.exe
using System;
using System.Reflection;
public static class Program
{
static void Main(string[] args)
{
AppDomain.CreateDomain("NewDomain").ExecuteAssembly("Test2.exe");
}
}
// In Test2.cs, compile to Test2.exe
using System;
using System.Diagnostics;
using System.Reflection;
class Test2
{
static void Main()
{
Console.WriteLine("Process: {0}",
Process.GetCurrentProcess().ProcessName);
Console.WriteLine("Entry assembly: {0}",
Assembly.GetEntryAssembly().CodeBase);
}
}
输出:
Process: Test
Entry assembly: file:///c:/Users/Jon/Test/Test2.EXE
Jon Skeet的好例子http://stackoverflow.com/a/8406779/37759 –