In a Visual Studio setup project, How do I generat

2019-01-10 03:04发布

I have a Visual Studio setup project. Upon installation, it creates an uninstall batch file in the application folder. IF the user wants to uninstall the product, he can go to "Add/Remove Programs", or he can just double-click the uninstall.cmd. The contents are:

%windir%\system32\msiexec /x {CC3EB7BF-DD82-48B9-8EC5-1B0B62B6D285}

The GUID there is the ProductCode from the Setup Project in Visual Studio.

ProductCode

But, in order for upgrades to work properly, I have to increment the Version number, every time I produce a new MSI. And, if I increment the Version number, then I also have to generate a new Guid for the ProductCode. Which means the static uninstall.cmd file needs to change.

How can I dynamically generate a batch file that contains the ProductCode for the, at build time?

8条回答
甜甜的少女心
2楼-- · 2019-01-10 03:04

I just made this work:

Add an uninstall.bat file to your project. The file contents is:

msiexec.exe /x %1

Make a shortcut to that batch file (say in User's Program Menu), specify in the shortcut's properties, next to Arguments: [ProductCode].

查看更多
仙女界的扛把子
3楼-- · 2019-01-10 03:10

For removing the application I would use the [ProductCode] as a parameter, calling the msiexec from within the application itself - for a detailed guide as to creating the uninstaller please check this blog: http://endofstream.com/creating-uninstaller-in-a-visual-studio-project/

查看更多
男人必须洒脱
4楼-- · 2019-01-10 03:13

a slight variation on the batch file approach. if you don't want to show a command window, use a wscript file. supply [ProductCode] as before in Arguments

<job>
<?job debug="true" error="true" ?>
<script language="JScript">
    var arg = [];
    for (var i=0; i<WSH.Arguments.length; i++) {
        arg.push( WSH.Arguments.Item(i) );
    }

    if (arg.length>0) {
        var productcode = arg[0];
        var v = new ActiveXObject("Shell.Application");
        v.ShellExecute("msiexec.exe", "/x "+productcode, "", "open", 10);
    }

    WSH.Quit(0);
</script>
</job>
查看更多
干净又极端
5楼-- · 2019-01-10 03:17

I found this solution here

"Using Visual Studio 2005/2008, you don’t need to write any code to add a uninstall option for a Setup project (Yes I know some people can write code to do it)

1) In the Setup Project –> File System windows –> Right Click “File System on Target machine” –> add a Special Folder, select System Folder;

2) Into this system folder Add a file. Browse for msiexec.exe from local System32 folder and add it. Override default properties of this file as follows:

Condition:=Not Installed (make sure you put ‘Not Installed’ exactly like that, same case and everything), Permanent:=True, System:=True, Transitive:=True, Vital:=False.

3) Create a new shortcut under the ‘Users Program Menu’, Set Target to the System Folder which you created in the step 1. and point it’s at the msiexec.exe. Rename the shortcut to ‘Uninstall Your Application’. Set the Arguments property to /x{space}[ProductCode].

4) Build the project, ignore warning about the fact that msiexec should be excluded, DONT exclude it or the setup project wont build.

The ‘Not Installed’ condition and Permananet:=True ensure that the msiexec.exe is only placed into the system folder as part of the install IF it doesn’t aready exist, and it is not removed on an uninstall - therefore it;s pretty safe to ignore that warning and just go for it.

(Based on the description from SlapHead)"

查看更多
Anthone
6楼-- · 2019-01-10 03:17

every time when u generate setup then always change product code. create a uninstaller shortcut and there u will find command line argument passing technique to sort cut. there u will write always "/u product code" product code u need to write here always.

put this code in main method of program.cs file

[STAThread]
        static void Main()
        {
            bool flag = true;
            string[] arguements = Environment.GetCommandLineArgs();
            foreach (string str in arguements)
            {
                if ((str.Split('='))[0].ToLower() == "/u")
                {
                    if (MessageBox.Show("Do you want to uninstall job board", "Are you Sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        flag = false;
                        RemoveRegSettings();
                        RemoveIniFile();
                        string guid = str.Split('=')[1];
                        string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
                        ProcessStartInfo si = new ProcessStartInfo(path + @"\msiexec.exe", "/i" + guid);
                        Process.Start(si);
                        Application.Exit();
                        return;
                    }
                }
            }
            //

            //************************************************
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
查看更多
我命由我不由天
7楼-- · 2019-01-10 03:20

I did a combination of the accepted answer and Locksmith's answer to not see any flashes of command prompt or the command prompt itself. One pro of doing it like this is that you DONT HAVE to create a shortcut and set the arguments for it in order for it to work.

My createUninstaller.js file:

var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("Scripting.FileSystemObject");

var parameters = Session.Property("CustomActionData").split("|"); 
var targetDir = parameters[0];
var productCode = parameters[1];

ts = fso.OpenTextFile(targetDir + "Uninstall.js", ForWriting, true);

ts.WriteLine("var v = new ActiveXObject(\"Shell.Application\");");
ts.WriteLine("v.ShellExecute(\"msiexec.exe\", \"/x "+productCode+"\", \"\", \"open\",10);");
ts.Close();

This file is added as a custom action at the commit action 'directory'. In order to actually get to this custom actions: right click your setup project>view>custom actions>right click commit 'directory'>add custom action. After you have to search for the createUninstaller.js file you created and add it.

Now to make the createUninstaller.js read the variables targetDir and productCode you have to
Right click the createUninstaller.js file in the setup project custom action 'commit' directory and go to properties window. In properties you will see the 'CustomActionData' property. In there you just copy paste [TARGETDIR]|[ProductCode]
And VOILA! It should now add the Uninstall.js file which will work as soon as you double click it.

查看更多
登录 后发表回答