以编程方式将程序注册到"添加/删除程序"并将文件存储在可执行文件中

Boa*_*rdy 18 c# install uninstall

我正在开发一个Windows c#console应用程序,我想让用户安装到他们的计算机上.

我想将自己的Windows Installer可执行文件作为Visual Studio中内置的安装部署工具,看起来在某种程度上缺乏自定义和文档的功能.

因此,因为我想创建自己的Windows安装程序,如何将我的程序注册到"添加/删除程序"窗口中,以便他们可以选择在需要时再次卸载它,并重新启动我的安装程序以执行删除操作.

同样,可执行文件显然需要将文件复制到PC上的各个位置,即C:\Program Files如何将可执行文件存储在Windows安装程序可执行文件中,以便将它们移动到正确的位置.

这可能吗?

感谢您的任何帮助,您可以提供.

Sam*_*eff 31

这是我们用于在添加/删除程序中注册程序的例程:

private void CreateUninstaller()
{
    using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(
                 @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", true))
    {
        if (parent == null)
        {
            throw new Exception("Uninstall registry key not found.");
        }
        try
        {
            RegistryKey key = null;

            try
            {
                string guidText = UninstallGuid.ToString("B");
                key = parent.OpenSubKey(guidText, true) ??
                      parent.CreateSubKey(guidText);

                if (key == null)
                {
                    throw new Exception(String.Format("Unable to create uninstaller '{0}\\{1}'", UninstallRegKeyPath, guidText));
                }

                Assembly asm = GetType().Assembly;
                Version v = asm.GetName().Version;
                string exe = "\"" + asm.CodeBase.Substring(8).Replace("/", "\\\\") + "\"";

                key.SetValue("DisplayName", "My Program");
                key.SetValue("ApplicationVersion", v.ToString());
                key.SetValue("Publisher", "My Company");
                key.SetValue("DisplayIcon", exe);
                key.SetValue("DisplayVersion", v.ToString(2));
                key.SetValue("URLInfoAbout", "http://www.blinemedical.com");
                key.SetValue("Contact", "support@mycompany.com");
                key.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd"));
                key.SetValue("UninstallString", exe + " /uninstallprompt");
            }
            finally
            {
                if (key != null)
                {
                    key.Close();
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(
                "An error occurred writing uninstall information to the registry.  The service is fully installed but can only be uninstalled manually through the command line.",
                ex);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @MacGyver,`B`表示"用连字符分隔的32位数,用括号括起来",这是注册表用于guids的格式.https://msdn.microsoft.com/en-us/library/97af8hh4(v=vs.110).aspx (3认同)
  • @MacGyver`UninstallRegKeyPath`是顶部`@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"`的字符串.我在方法的顶部对其进行了硬编码,但没有注意到错误处理中存在对变量的引用.guid只是安装人员的指南.您为应用生成一个guid并在每次安装时使用相同的guid,这是任何Windows应用程序安装的正常部分.它只需要在您的应用程序版本之间保持一致,以唯一标识您的应用程序(除非您支持多个并排版本,然后对于版本中的版本是唯一的). (3认同)
  • 因此,源代码中未定义"UninstallGuid"和"UninstallRegKeyPath".我假设第二个是卸载应用程序的exe的路径.你能评论这两个吗?我也可以发一个问题撤消这个reg键,但那是我的第三个也是最后一个问题. (2认同)