简体   繁体   English

如何获取C#控制台应用程序的.exe名称?

[英]How do I get the .exe name of a C# console application?

I'm debugging "xiixtasks.exe", a C# console-mode application in VS2008. 我正在调试VS2008中的C#控制台模式应用程序“xiixtasks.exe”。

I'm trying to get the version info from xiixtasks.exe. 我正在尝试从xiixtasks.exe获取版本信息。

When I try "Process.GetCurrentProcess()", it gives me the filename and version info for vshost.exe, NOT xiixtasks.exe: 当我尝试“Process.GetCurrentProcess()”时,它为我提供了vshost.exe的文件名和版本信息,而不是xiixtasks.exe:

  // WRONG: this gives me xiixtasks.vhost.exe, version 9.0.30729.1
  //        I *want* "xiixtasks.exe", version 1.0.0.1024
  System.Diagnostics.FileVersionInfo fi =
    System.Diagnostics.Process.GetCurrentProcess().MainModule.FileVersionInfo;

What should I be doing instead? 我应该做什么呢?

Thank you in advance! 先感谢您!

====================================================== ================================================== ====

Solution: 解:

1) The initial problem was indeed the IDE's "vshost" wrapper. 1)最初的问题确实是IDE的“vshost”包装器。 One workaround would have been to change the build settings. 一种解决方法是更改​​构建设置。

2) Assembly.GetExecutingAssembly().CodeBase is an excellent solution - thank you!. 2)Assembly.GetExecutingAssembly()。CodeBase是一个很好的解决方案 - 谢谢! It works inside and outside the debugger. 它在调试器内部和外部工作。

3) Unfortunately, when I tried calling it with a function that expected a normal file path (instead of a URI like GetExecutingAssembly()" gives you), it died with a "Uri formats are not supported" exception. 3)不幸的是,当我尝试用一​​个预期正常文件路径的函数调用它(而不是像GetExecutingAssembly()“给你”这样的URI)时,它死于“不支持Uri格式”异常。

4) Final solution: call GetExecutingAssembly(), then Uri.LocalPath (): 4)最终解决方案:调用GetExecutingAssembly(),然后调用Uri.LocalPath():

...
else if (cmdArgs.cmd.Equals(CmdOptions.CMD_SHOW_VERSION))
{
    string codeBaseUri = 
       Urifile.System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
    string codeBase =
        new Uri (codeBaseUri).LocalPath;
    string sVersion = Util.GetWindowsVersion(codeBase);
    System.Console.WriteLine ("version({0}): {1}: ",
        Util.Basename(codeBase), sVersion);
}

Thank you once again, all! 全部再次感谢你!

Full Path of your assembly: 装配的完整路径:

Assembly.GetExecutingAssembly().CodeBase.Dump();

You can always extract the name with Path.GetFileName : 您始终可以使用Path.GetFileName提取名称:

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
string name = Path.GetFileName(codeBase);

It sounds like you are running inside the IDE in debug with the "Enable the Visual Studio hosting process" checkbox enabled. 听起来您正在调试IDE中运行,并启用了“启用Visual Studio托管过程”复选框。 In which case, the current process is xiixtasks.vshost.exe - it is a shell exe used to help debugging. 在这种情况下,当前进程 xiixtasks.vshost.exe - 它是一个用于帮助调试的shell exe。 You need to disable that checkbox. 您需要禁用该复选框。

在此输入图像描述

Depending on what info you want, you can use a variety of calls: 根据您想要的信息,您可以使用各种电话:

在此输入图像描述

Try this to get the version: 试试这个来获得版本:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version

And this to get the name: 这就是这个名字:

System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()

This would also get you name / version 这也会得到你的名字/版本

name: Assembly.GetExecutingAssembly().GetName().Name
version: = Assembly.GetExecutingAssembly().GetName().Version

That's actually what I would expect as it is in fact executing the vshost file in debug mode. 这实际上是我所期望的,因为它实际上是在调试模式下执行vshost文件。

If you don't want it to execute the vshost file but rather your exe directly, you need to go into your project settings and disable the vshost debugging option. 如果您不希望它直接执行vshost文件而是执行exe,则需要进入项目设置并禁用vshost调试选项。

'vshost.exe'包装器通常只对Debug版本启用,也没有必要使用它,所以也许您应该考虑在项目设置下关闭它(Debug选项卡,取消选中'启用Visual Studio托管进程'在底部)。

If you will get the exception like "URI format is not supported" While Copying the xml file from one directory to the another by using the following code. 如果您将获得类似“不支持URI格式”的异常,则使用以下代码将xml文件从一个目录复制到另一个目录。

string path=System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
DirectoryInfo dir=new DirectoryInfo(path+"\\App2");
FileInfo[] Files=dir.GetFiles();

then convert it into the URI like. 然后将其转换为URI。

string path =Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); 
URI uri = new URI(path); 
string FileName = Path.Combine(uri.LocalPath, "\\App2"+file.Name);

then use it to get the files. 然后用它来获取文件。

尝试

var asm = Assembly.GetExecutingAssembly();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM