如何以编程方式获取 Windows 服务的版本信息
我需要以编程方式获取 Windows 服务的版本并将其存储在字符串中。然后,我会将版本附加到 ProjectInstaller 类中的显示名称和服务名称。现在我得到一个空字符串,并且在调试我的安装项目时遇到问题。这是我当前的代码:
string version = null;
try
{
Assembly exeAssembly = Assembly.GetEntryAssembly();
Type attrType = typeof(AssemblyFileVersionAttribute);
object[] attributes = exeAssembly.GetCustomAttributes(attrType, false);
if (attributes.Length > 0)
{
AssemblyFileVersionAttribute verAttr = (AssemblyFileVersionAttribute)attributes[0];
if (verAttr != null)
{
version = verAttr.Version;
}
}
}
catch
{
}
if (version == null)
{
version = string.empty;
}
I need to obtain the version of my Windows service programmatically and store it in a string. Then, I'll append the version to my display name and service name in the ProjectInstaller class. Right now I'm getting an empty string and I'm having trouble debugging my setup project. Here's my current code:
string version = null;
try
{
Assembly exeAssembly = Assembly.GetEntryAssembly();
Type attrType = typeof(AssemblyFileVersionAttribute);
object[] attributes = exeAssembly.GetCustomAttributes(attrType, false);
if (attributes.Length > 0)
{
AssemblyFileVersionAttribute verAttr = (AssemblyFileVersionAttribute)attributes[0];
if (verAttr != null)
{
version = verAttr.Version;
}
}
}
catch
{
}
if (version == null)
{
version = string.empty;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将以 1.0.0.0 形式返回。
或者,您可以使用
version.Major + “.” + version.Minor
仅获取前两个数字。或者,如果您想要文件版本...
Will return it in 1.0.0.0 form.
Or, you can use the
version.Major + "." + version.Minor
to get just the first two numbers.Alternatively, if you want the file version...