我正在调试 VS2008 中的 C# 控制台模式应用程序“xiixtasks.exe”。
我正在尝试从 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;
我应该怎么做?
先感谢您!
==================================================== ====
解决方案:
1) 最初的问题确实是 IDE 的“vshost”包装器。一种解决方法是更改构建设置。
2) Assembly.GetExecutingAssembly().CodeBase 是一个很好的解决方案 - 谢谢!它在调试器的内部和外部工作。
3) 不幸的是,当我尝试使用一个期望正常文件路径的函数调用它时(而不是像 GetExecutingAssembly()" 给你的 URI),它因“不支持 Uri 格式”异常而死。
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);
}
再次感谢大家!