28

我有一个 Visual Studio 安装项目。安装后,它会在应用程序文件夹中创建一个卸载批处理文件。如果用户想卸载产品,他可以去“添加/删除程序”,或者他可以直接双击卸载.cmd。内容是:

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

那里的 GUID 是 Visual Studio 中安装项目的 ProductCode。

产品代码

但是,为了使升级正常工作,我必须在每次生成新的 MSI 时增加版本号。而且,如果我增加版本号,那么我还必须为 ProductCode 生成一个新的 Guid。这意味着静态卸载.cmd 文件需要更改。

如何在构建时动态生成包含 ProductCode 的批处理文件?

4

8 回答 8

67

我在这里找到了这个解决方案

“使用 Visual Studio 2005/2008,您无需编写任何代码即可为安装项目添加卸载选项(是的,我知道有些人可以编写代码来执行此操作)

1)在Setup Project中——>File System windows——>右键“File System on Target machine”——>添加一个Special Folder,选择System Folder;

2) 在这个系统文件夹中添加一个文件。从本地 System32 文件夹中浏览 msiexec.exe 并添加它。覆盖此文件的默认属性,如下所示:

条件:=未安装(确保您将“未安装”完全一样,相同的情况和所有内容),永久:=真,系统:=真,传递:=真,重要:=假。

3) 在“用户程序菜单”下创建一个新的快捷方式,将目标设置为您在步骤 1 中创建的系统文件夹。并将其指向 msiexec.exe。将快捷方式重命名为“卸载您的应用程序”。将 Arguments 属性设置为 /x{space}[ProductCode]。

4)构建项目,忽略有关应该排除 msiexec 的警告,不要排除它,否则安装项目将无法构建。

“未安装”条件和 Permananet:=True 确保 msiexec.exe 仅作为安装的一部分放置到系统文件夹中,如果它不存在,并且在卸载时不会被删除 - 因此它;s忽略该警告并继续执行它是非常安全的。

(根据 SlapHead 的描述)"

于 2009-09-02T11:55:50.590 回答
10

我刚刚完成了这项工作:

将uninstall.bat 文件添加到您的项目中。文件内容为:

msiexec.exe /x %1

为该批处理文件创建一个快捷方式(例如在用户的程序菜单中),在快捷方式的属性中指定,在 Arguments: 旁边[ProductCode]

于 2011-04-23T14:26:56.797 回答
4

这是我编写的创建uninstall.cmd 的脚本。它在安装期间作为自定义操作运行。

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.cmd", ForWriting, true);

ts.WriteLine("@echo off");
ts.WriteLine("goto START");
ts.WriteLine("=======================================================");
ts.WriteBlankLines(1);
ts.WriteLine(" Uninstall.cmd");
ts.WriteBlankLines(1);
ts.WriteLine("=======================================================");
ts.WriteBlankLines(1);
ts.WriteLine(":START");
ts.WriteLine("@REM The uuid is the 'ProductCode' in the Visual Studio setup project");
ts.WriteLine("%windir%\\system32\\msiexec /x " + productCode);
ts.WriteBlankLines(1);
ts.Close();

结果是一个始终包含当前 ProductCode 的 cmd 文件。

这个的缺点是什么?创建uninstall.cmd 的脚本保留在安装目录中。不是一个大问题,但我不喜欢安装目录中的垃圾。我还没有尝试让“createInstaller.js”自删除。那可能行得通。

编辑:是的,使 createInstaller.js 自删除工作正常。

我要接受我自己的答案!

于 2009-08-31T08:42:41.267 回答
2

批处理文件方法略有不同。如果您不想显示命令窗口,请使用 wscript 文件。像以前一样在 Arguments 中提供 [ProductCode]

<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>
于 2012-01-27T04:12:59.350 回答
1

为了删除应用程序,我将使用 [ProductCode] 作为参数,从应用程序本身调用 msiexec - 有关创建卸载程序的详细指南,请查看此博客:http ://endofstream.com/creating-uninstaller-在-a-visual-studio-project/

于 2010-08-30T19:04:52.573 回答
1

我意识到这个问题已经得到解答,但我所做的是创建一个包含以下内容的批处理文件:

start msiexec.exe /x %1

使用“开始”可防止命令提示符在卸载过程中保持打开状态,并且 %1 将替换为执行时传递给批处理文件的第一个参数。

我将批处理文件添加到应用程序文件夹并创建了一个指向批处理文件的菜单快捷方式。如果您右键单击快捷方式并选择属性窗口。在那里,将以下文本添加到 Arguments 属性:

[ProductCode]

构建并运行安装程序后,您将拥有一个开始菜单快捷方式,该快捷方式调用批处理文件并传递构建该应用程序版本时的产品代码。然后,用户将看到询问是否应卸载产品的提示。

这个解决方案唯一的缺点是如果用户浏览到应用程序文件夹并双击批处理文件,卸载程序将给出安装包不存在的错误。

于 2013-03-07T13:34:31.517 回答
1

我将接受的答案和 Locksmith 的答案结合起来,没有看到任何命令提示符或命令提示符本身的闪烁。这样做的一个优点是您不必创建快捷方式并为其设置参数以使其正常工作。

我的 createUninstaller.js 文件:

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

此文件作为自定义操作添加到提交操作“目录”中。为了实际获得此自定义操作:右键单击您的设置项目>查看>自定义操作>右键单击提交“目录”>添加自定义操作。在您必须搜索您创建的 createUninstaller.js 文件并添加它之后。

现在要让 createUninstaller.js 读取变量 targetDir 和 productCode,您必须
右键单击设置项目自定义操作“提交”目录中的 createUninstaller.js 文件并转到属性窗口。在属性中,您将看到“CustomActionData”属性。在那里你只需复制粘贴 [TARGETDIR]|[ProductCode]
瞧!它现在应该添加 Uninstall.js 文件,只要你双击它就可以工作。

于 2018-07-25T02:05:22.220 回答
0

每次您生成设置时,请始终更改产品代码。创建一个卸载程序快捷方式,您将在那里找到命令行参数传递技术来排序剪切。你会一直写“/u产品代码”产品代码你需要一直写在这里。

将此代码放在 program.cs 文件的 main 方法中

[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);
于 2013-01-29T12:50:57.080 回答