简体   繁体   中英

How to insert a value of data type 'Formatted' into the MSI database

I am trying to add a custom action in the CustomAction table of an msi.The problem is that the column name Target is of data type Formatted.It is failing when i am trying to insert a string into the column since the requested type is 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();   

It is always throwing exception.I am able to add values into Action Type without any problem.The problem comes with Target which is of formatted data type.

If it's the view.Execute that throws the exception, then say so, just to be explicit. Also somewhere in the exception itself (or the inner exception) there should be an MSI-related error number or text that says something about the details of the exception in MSI terms.

Anyway, Target is Formatted, which is text. The maximum length of a text string is 255 characters, and the length of your text is more than 255 characters by my count. That's most likely the issue. You could test with a very small string to verify. Your alternative would be to insert the script into the Binary table and alter the custom action accordingly.

i dont reformat the string so check it before using it :

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM