我如何获得一个C#控制台应用程序的

我如何获得一个C#控制台应用程序的

本文介绍了我如何获得一个C#控制台应用程序的.exe名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调试xiixtasks.exe,在VS2008一个C#控制台模式的应用程序。

I'm debugging "xiixtasks.exe", a C# console-mode application in VS2008.

我想从xiixtasks.exe版本信息

I'm trying to get the version info from xiixtasks.exe.

当我尝试Process.GetCurrentProcess(),它给了我vshost.exe文件名和版本信息,而不是xiixtasks.exe:

When I try "Process.GetCurrentProcess()", it gives me the filename and version info for vshost.exe, NOT 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?

预先感谢您!

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

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

解决方案:

1)最初的问题确实是IDE的vshost包装。
一个解决办法将是改变构建设置。

1) The initial problem was indeed the IDE's "vshost" wrapper. One workaround would have been to change the build settings.

2)Assembly.GetExecutingAssembly()。基本代码是卓越的的解决方案- 谢谢!。
它的工作原理内部和调试器外部。

2) Assembly.GetExecutingAssembly().CodeBase is an excellent solution - thank you!. It works inside and outside the debugger.

3)不幸的是,当我试图与预期一个正常的文件路径(而不是一个URI像GetExecutingAssembly()给你)一个函数调用它,它死了。一个URI格式不支持异常

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.

4)最终的解决方案:调用GetExecutingAssembly(),然后Uri.LocalPath():

4) Final solution: call GetExecutingAssembly(), then 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);
}

感谢您再次,所有的!

推荐答案

您组装完整路径:

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

您可以随时提取名为 Path.GetFileName

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

这篇关于我如何获得一个C#控制台应用程序的.exe名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:04