我想获取当前正在运行的程序的名称,即程序的可执行文件名称.在C/C++中,你可以从中获得它args[0].
Assembly.GetEntryAssembly()不适用于Web应用程序.
但是......我真的需要这样的东西.我使用了一些在Web和非Web应用程序中使用的深层嵌套代码.
我目前的解决方案是浏览StackTrace以找到第一个被调用的程序集.
/// <summary>
/// Version of 'GetEntryAssembly' that works with web applications
/// </summary>
/// <returns>The entry assembly, or the first called assembly in a web application</returns>
public static Assembly GetEntyAssembly()
{
// get the entry assembly
var result = Assembly.GetEntryAssembly();
// if none (ex: web application)
if (result == null)
{
// current method
MethodBase methodCurrent = null;
// number of frames to skip
int framestoSkip = 1;
// loop until we cannot got further in …Run Code Online (Sandbox Code Playgroud) 我是.NET用户,我的目标就像找到主执行程序集(EXE文件)目录的绝对路径一样简单.
我有几个候选人:
Assembly.GetExecutingAssembly().CodeBaseAssembly.GetExecutingAssembly().LocationAppDomain.CurrentDomain.BaseDirectory如果要通过.NET文档判断 - 我倾向于CodeBase.任何人都可以用比.NET文档更具体的术语来解释这三个问题吗?一个例子来证明这种差异?
我有一个MyProgram.EXE引用Utilities程序集的控制台应用程序().
在我的Utilities程序集中,我有以下代码:
Dim asm As Assembly = Assembly.GetExecutingAssembly()
Dim location As String = asm.Location
Dim appName As String = System.IO.Path.GetDirectoryName(location)
Conole.WriteLine("AppName is: {0}", appName)
Run Code Online (Sandbox Code Playgroud)
当我打电话给它时MyProgram.EXE,我会收到" AppName is: Utilities.dll"
我想要的是" AppName is: MyProgram.EXE"
我究竟做错了什么?
首先,我想说我并不是指完整路径,
GetModuleFileName或argv [0]产生的.有没有一个更聪明的解决方案,而不是在最后一次反斜杠之前解雇所有内容?
.net ×2
c# ×2
assemblies ×1
c++ ×1
command-line ×1
directory ×1
exe ×1
path ×1
reflection ×1
stack-frame ×1
vb.net ×1
windows ×1