当前位置: 首页 > 编程语言 > C#

如何在c#中读取MSI属性分享

时间:2023-04-11 11:07:27 C#

C#学习教程:如何在c#中读取MSI属性我使用以下代码:publicstaticstringGetMSIProperty(stringmsiFile,stringmsiProperty){stringretVal=string.Empty;输入classType=Type.GetTypeFromProgID("WindowsInstaller.Installer");对象installerObj=Activator.CreateInstance(classType);Windows安装程序。安装程序installer=installerObjasWindowsInstaller.Installer;数据库database=installer.OpenDatabase("C:\DataP\sqlncli.msi",0);stringsql=String.Format("SELECTValueFROMPropertyWHEREProperty='{0}'",msiProperty);查看视图=数据库.OpenView(sql);记录record=view.Fetch();if(record!=null){retVal=record.get_StringData(1);}elseretVal="找不到属性";返回值;但我得到一个错误,因为System.Runtime.InteropServices.COMException未处理。sqlncli.msi文件实际上位于c:\DataP位置。调试时我发现数据库不包含installer.OpenDatabase()语句后的数据。请建议我如何解决这个问题并在C#中获取MSI属性。提前致谢。WindowsInstallerXML(WiXDTF)的部署工具基础是Microsoft的一个开源项目,其中包括Microsoft.Deployment.WindowsInstallerMSI互操作库。使用它执行这些类型的查询要容易得多,也更可靠。它甚至有一个LINQtoMSI提供程序,允许您将MSI表视为实体并针对它们编写查询。使用系统;使用System.Linq;使用Microsoft.Deployment.WindowsInstaller;使用Microsoft.Deployment.WindowsInstaller.Linq;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){using(vardatabase=newQDatabase(@"C:tfsiswix.msi",DatabaseOpenMode.ReadOnly)){varproperties=frompindatabase.Propertiesselectp;foreach(属性中的var属性){Console.WriteLine("{0}={1}",property.Property,property.Value);}}using(vardatabase=newDatabase(@"C:tfsiswix.msi",DatabaseOpenMode.ReadOnly)){using(varview=database.OpenView(database.Tables["Property"].SqlSelectString)){view.Execute();foreach(varrecinview)using(rec){Console.WriteLine("{0}={1}",rec.GetString("Property"),rec.GetString("Value"));}}}控制台.Read();}}}SQL字符串不正确。它应该是:SELECT`Value`FROM`Property`WHERE`Property`.`Property`='{0}'Devashri工作:stringsql=String.Format("SELECT`Value`FROM`Property`WHERE`Property`='{0}'",msiProperty);注意单引号!我是通过以下方式完成的:StringinputFile=@"C:\Rohan\sqlncli.msi";//获取WindowsInstaller对象的类型TypeinstallerType=Type.GetTypeFromProgID("WindowsInstaller.Installer");//创建WindowsInstaller对象WindowsInstaller.Installerinstaller=(WindowsInstaller.Installer)Activator.CreateInstance(installerType);//在输入文件中打开MSI数据库Databasedatabase=installer.OpenDatabase(inputFile,0);//在版本属性的属性表上打开一个视图Viewview=database.OpenView("SELECT*FROM_Tables");//执行视图查询view.Execute(null);//从视图中获取记录Recordrecord=view.Fetch();while(record!=null){Console.WriteLine(record.get_StringData(0)+'='+record.get_StringData(1)+'='+record.get_StringData(2)+'='+record.get_StringData(3));记录=view.Fetch();对我有用以上就是C#学习教程:HowtoreadallthecontentsharedbyMSIinc#inc#,如果对你有用,需要进一步了解C#学习教程,希望大家多多关注给它。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢