如何添加我的程序来添加/删除程序? VB.NET

时间:2013-08-01 11:52:38

标签: vb.net registry install control-panel

我正在winforms中创建一个vb.net安装,我希望我的应用程序将像其他程序一样添加到控制面板 - >程序 - >程序,功能和功能及其信息。

我尝试使用以下代码执行此操作:

My.Computer.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall\Myapp").SetValue("Display Name", "Appname")
My.Computer.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall\Myapp").SetValue("Display Version", "1.0.0.0")
My.Computer.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall\Myapp").SetValue("Publisher", "SomePublisher")
My.Computer.Registry.LocalMachine.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall\Myapp").SetValue("Uninstall Path", "uninstpath")

我的代码有什么问题,我该如何解决?

2 个答案:

答案 0 :(得分:1)

这是一种不使用cmd,shell或批处理文件的方法:

首先,您需要允许您的安装程序 - 应用程序在注册表中写入。您可以通过设置" requestedExecutionLevel"来实现这一目标。 to" highestAvailable":

文件中的

:" app.manifest"

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

现在您可以使用DotNetFramework函数创建卸载RegistryKey:

        'Setting My Values
        Dim ApplicationName As String = "Yourapp"
        Dim ApplicationVersion As String = "1.0.0.0"
        Dim ApplicationIconPath As String = "%PROGRAMFILES%\Yourapp\yourapp.ico"
        Dim ApplicationPublisher As String = "Yourname"
        Dim ApplicationUnInstallPath As String = "%PROGRAMFILES%\Yourapp\UNINSTALL.exe"
        Dim ApplicationInstallDirectory As String = "%PROGRAMFILES%\Yourapp\"

        'Openeing the Uninstall RegistryKey (don't forget to set the writable flag to true)
        With My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall", True)

            'Creating my AppRegistryKey
            Dim AppKey As Microsoft.Win32.RegistryKey = .CreateSubKey(ApplicationName)

            'Adding my values to my AppRegistryKey
            AppKey.SetValue("DisplayName", ApplicationName, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("DisplayVersion", ApplicationVersion, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("DisplayIcon", ApplicationIconPath, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("Publisher", ApplicationPublisher, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("UninstallString", ApplicationUnInstallPath, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("UninstallPath", ApplicationUnInstallPath, Microsoft.Win32.RegistryValueKind.String)
            AppKey.SetValue("InstallLocation", ApplicationInstallDirectory, Microsoft.Win32.RegistryValueKind.String)

        End With

答案 1 :(得分:0)

我找到了间接的方法 - 通过cmd。这不是最好的方式,但可能有效。

您可以在资源中将以下文本写为name.cmd:

'sets the name of your app to display
reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /v "DisplayName" /d "Yourapp" /f 
'sets your app version to display
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "DisplayVersion" /D "1.0.0.0"
'sets your app icon to display location
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "DisplayIcon" /D "%PROGRAMFILES%\Yourapp\yourapp.ico"
'sets your name to display as publisher
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "Publisher" /D "Yourname"
'sets the uninstall path
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "UninstallString" /D "%PROGRAMFILES%\Yourapp\UNINSTALL.exe"
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "UninstallPath" /D "%PROGRAMFILES%\Yourapp\UNINSTALL.exe"
' sets the install directory
REG ADD "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Yourapp" /V "InstallLocation" /D "%PROGRAMFILES%\Yourapp\"

然后,你以cmd方式打开它:

Dim procInfo As New ProcessStartInfo()

procInfo.UseShellExecute = True
'writes the file name.cmd to appdata
IO.File.WriteAllText(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "/name.cmd", My.Resources.name)
'sets the path of the file to be opened to name.cmd
procInfo.FileName = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "/name.cmd"

procInfo.WorkingDirectory = ""
procInfo.Verb = "runas"
'running the file in cmd
Process.Start(procInfo)