本文介绍了为什么我不能使用此代码行获取文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





当我运行程序ProgB时,我需要获取程序ProgA的属性。

I在Windows资源管理器中右键单击ProgA并获取其信息并转到属性/详细信息:



文件描述驱动程序ProgA

类型应用程序

文件版本4.0.0.0

产品名称ProgA

产品版本4.0.0.0

版权所有abc软件

大小653 KB

修改日期4/19/2016 11:30 PM

语言英语(美国)

原始文件名ProgA



这也是我想要/需要从这个代码行获得的信息







但是,当我在ProgB中使用下面的代码时,我只得到这个信息(这个信息是我在Visual Studio中调试时得到的)



Hi,

I need to get the properties of program "ProgA" when I am running program "ProgB".
I get his info when right clicking over "ProgA" in Windows Explorer and go to Properties/Details:

File description Driver ProgA
Type Application
File version 4.0.0.0
Product name ProgA
Product version 4.0.0.0
Copyright abc software
Size 653 KB
Date modified 4/19/2016 11:30 PM
Language English (United States)
Original filename ProgA

This is also the info I want/need to get from this codelines



But, when I use this code below in "ProgB" I get only this info ( this info is what I get when I am debugging in Visual Studio)

-		versInfo	{System.Diagnostics.FileVersionInfo}	System.Diagnostics.FileVersionInfo
		Comments	""	string
		CompanyName	""	string
		FileBuildPart	0	int
		FileDescription	""	string
		FileMajorPart	4	int
		FileMinorPart	0	int
+		FileName	'versInfo.FileName' threw an exception of type 'System.ArgumentException'	string {System.ArgumentException}
		FilePrivatePart	0	int
		FileVersion	""	string
		InternalName	""	string
		IsDebug	false	bool
		IsPatched	false	bool
		IsPreRelease	false	bool
		IsPrivateBuild	false	bool
		IsSpecialBuild	false	bool
		Language	"English (United States)"	string
		LegalCopyright	""	string
		LegalTrademarks	""	string
		OriginalFilename	""	string
		PrivateBuild	""	string
		ProductBuildPart	0	int
		ProductMajorPart	4	int
		ProductMinorPart	0	int
		ProductName	""	string
		ProductPrivatePart	0	int
		ProductVersion	""	string
		SpecialBuild	""	string







我做错了什么?






What am I doing wrong?

I do not get any error message in "ProgB", but I do not get the same info  with the code as I get when using Windows Explorer to check the "ProgA" properties.





我尝试过:





What I have tried:

var versInfo = FileVersionInfo.GetVersionInfo("ProgA.exe");

  String fileVersion = versInfo.FileVersion;
  String productVersion = versInfo.ProductVersion;
  string productName = versInfo.ProductName;

推荐答案

FileVersionInfo versionInfo    = null;
string          fileVersion    = string.Empty;
string          productVersion = string.Empty;
string          productName    = string.Empty;

try
{
    versionInfo    = FileVersionInfo.GetVersionInfo(@"c:\some folder\ProgA.exe");
    fileVersion    = versInfo.FileVersion;
    productVersion = versInfo.ProductVersion;
    productName    = versInfo.ProductName;
}
catch (Exception ex)
{
    // if an exception is thrown, you can inspect it here by
    // setting a breakpoint on either of the curly braces
}


这篇关于为什么我不能使用此代码行获取文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 00:18