這是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 –