如何插入数据类型的值'格式化'进入MSI数据库

时间:2016-01-15 13:42:19

标签: c# windows windows-installer

我正在尝试在msi的CustomAction表中添加自定义操作。问题是列名称Target的数据类型为Formatted。当我尝试将字符串插入列中时,它会失败type是Formatted。

WindowsInstaller.Installer ins = (WindowsInstaller.Installer)new Installer();

string strFileMsi = @"D:\Pack.msi";


 Database db3 = ins.OpenDatabase(strFileMsi, WindowsInstaller.MsiOpenDatabaseMode.msiOpenDatabaseModeDirect);
            System.Console.WriteLine("testing 1");



  WindowsInstaller.View vw3 = db3.OpenView("INSERT INTO CustomAction (Action,Type,Target) VALUES ('DeleteAction',3174,'\'Option Explicit\n\n'On Error Resume Next\nDim objFSO, strappfolder, WshShell, strprogramfiles, WshProcessenv\nDim intFilesCount, intSubFolderCount, intFileCount, objGetFolder, ObjFolder\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\") \nset WshShell = CreateObject(\"Wscript.Shell\")\nset WshProcessenv = WshShell.Environment(\"Process\")\nstrprogramfiles = WshProcessenv(\"PROGRAMFILES(x86)\")\n\nIf ObjFSO.FileExists(strprogramfiles & \"\\datavision -BMS\\err.txt\") Then\nobjFSO.DeleteFile strprogramfiles & \"\\datavision -BMS\\err.txt\"\nEnd If\n\nIf ObjFSO.FileExists(strprogramfiles & \"\\datavision -BMS\\out.txt\") Then\nobjFSO.DeleteFile strprogramfiles & \"\\datavision -BMS\\out.txt\"\nEnd If\n\n If ObjFSO.FolderExists(strprogramfiles & \"\\datavision -BMS\") Then\n\n    Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n\n   Set objGetFolder = objFSO.GetFolder(strprogramfiles & \"\\datavision -BMS\")\n\n    ObjFSO.DeleteFolder strprogramfiles & \"\\datavision -BMS\"\n   End If')");

            vw3.Execute(null);
            System.Console.WriteLine("testing");

             db3.Commit();
            vw3.Close();   

总是抛出异常。我能够在没有任何问题的情况下将值添加到Action Type中。问题来自Target,它是格式化的数据类型。

2 个答案:

答案 0 :(得分:1)

如果它是view.Execute抛出异常,那么这样说,只是为了明确。在异常本身(或内部异常)的某处,应该有一个与MSI相关的错误号或文本,其中说明了MSI术语中的异常细节。

无论如何,Target是Formatted,即文本。文本字符串的最大长度为255个字符,根据我的计数,文本的长度超过255个字符。这很可能就是问题所在。您可以使用非常小的字符串进行测试以进行验证。您可以选择将脚本插入二进制表并相应地更改自定义操作。

答案 1 :(得分:0)

我不重新格式化字符串,所以在使用它之前检查它:

WindowsInstaller.View vw3 = db3.OpenView("SELECT * FROM CustomAction");

        vw3.Execute(null);

        Record record = ins.CreateRecord(4);

        record.StringData[1] = "DeleteAction";
        record.IntegerData[2] = 3174;
        record.StringData[4] = "Option Explicit\n\n'On Error Resume Next\nDim objFSO, strappfolder, WshShell, strprogramfiles, WshProcessenv\nDim intFilesCount, intSubFolderCount, intFileCount, objGetFolder, ObjFolder\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\") \nset WshShell = CreateObject(\"Wscript.Shell\")\nset WshProcessenv = WshShell.Environment(\"Process\")\nstrprogramfiles = WshProcessenv(\"PROGRAMFILES(x86)\")\n\nIf ObjFSO.FileExists(strprogramfiles & \"\\datavision -BMS\\err.txt\") Then\nobjFSO.DeleteFile strprogramfiles & \"\\datavision -BMS\\err.txt\"\nEnd If\n\nIf ObjFSO.FileExists(strprogramfiles & \"\\datavision -BMS\\out.txt\") Then\nobjFSO.DeleteFile strprogramfiles & \"\\datavision -BMS\\out.txt\"\nEnd If\n\n If ObjFSO.FolderExists(strprogramfiles & \"\\datavision -BMS\") Then\n\n    Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n\n   Set objGetFolder = objFSO.GetFolder(strprogramfiles & \"\\datavision -BMS\")\n\n    ObjFSO.DeleteFolder strprogramfiles & \"\\datavision -BMS\"\n   End If";

        vw3.Modify(MsiViewModify.msiViewModifyInsert, record);