C#如何獲取或修改msi文件中的屬性-install

首先,Msi是個數據庫,也是第一次知道,用Orca數據庫編輯工具能查看這個數據庫中的各個表和字段;

知道了這些之後,就是如何在代碼裏實現了:

1、引用Microsoft Windows Installer Object Library這個Com組件,用它來操作這個數據庫;可能會遇到引用失敗,提示未能引用這個類,我就是一直引用不上,後來鼠標放到這個引用上,發現引用的是個本地的dll文件(C:\Windows\System32\msi.dll),直接把該文件拷貝到項目下,能正常引用,氣不氣;

2、然後就是代碼,像這樣:

     //獲取msi的版本號

     private string GetMsiVersion(string installerPath)
        {
            Type t = Type.GetTypeFromProgID("WindowsInstaller.Installer");
            Installer inst = (Installer)Activator.CreateInstance(t);
            Database d = inst.OpenDatabase(
                installerPath, 
                MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
            View v = d.OpenView(
                "SELECT * FROM Property WHERE Property = 'ProductVersion'");
            v.Execute(null);
            Record r = v.Fetch();
            string result = r.get_StringData(2);
            return result;
        }
上面是獲取的,修改的話,這樣:

Type t = Type.GetTypeFromProgID("WindowsInstaller.Installer");
WindowsInstaller.Installer inst = (WindowsInstaller.Installer)Activator.CreateInstance(t);
Database d = inst.OpenDatabase(
installerPath,
MsiOpenDatabaseMode.msiOpenDatabaseModeTransact);
WindowsInstaller.View v = d.OpenView(
"select Target from CustomAction where Source='TARGETDIR'");
v.Execute(null);
Record r = v.Fetch();
r.StringData[0] = "D:\\TestZdc";
v.Modify(MsiViewModify.msiViewModifyUpdate, r);
v.Fetch();
v.Close();
d.Commit();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章