我正在编写一个类似于 Google 桌面的桌面应用程序,但使用我自己的带有 vb.net 2008 的小工具,当用户将其安装在他们的计算机上以在启动时运行时,我如何制作我的应用程序?
假设我的应用程序名称是 windowsAplication1 并且我使用的是 windows XP 并且该程序将安装在 C 盘上?
您可以使用以下代码将其添加到注册表
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
你可以使用删除它
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)
上面的代码会将它添加到所有用户。您可以在以下键中将其添加到当前用户
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
或者,您可以在“启动”文件夹中添加指向您的应用程序的链接。
我建议你不要自动这样做,它可能会激怒用户。我讨厌应用程序自动将它们添加到 Windows 启动。为用户提供在 Windows 启动时运行程序的选项。
Simpley 使用以下代码:
Dim info As New FileInfo(application.startuppath)
info.CopyTo(My.Computer.FileSystem.SpecialDirectories.Programs + "\startup\myapp.exe")
希望能帮助到你。
您可以使用以下代码执行此操作
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This is where you'll need to have the program
' set the check box to the previous selection that
' the user has set. It's up to you how you do this.
' For now, I'll set it as "unchecked".
CheckBox1.Checked = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' The following code is a rendition of one provided by
' Firestarter_75, so he gets the credit here:
Dim applicationName As String = Application.ProductName
Dim applicationPath As String = Application.ExecutablePath
If CheckBox1.Checked Then
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.SetValue(applicationName, """" & applicationPath & """")
regKey.Close()
Else
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.DeleteValue(applicationName, False)
regKey.Close()
End If
' Assuming that you'll run this as a setup form of some sort
' and call it using .ShowDialog, simply close this form to
' return to the main program
Close()
End Sub
一个简单的方法
Imports Microsoft.Win32
...
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run",True)
regKey.SetValue(Application.ProductName, Application.ExecutablePath)
regKey.Close()
希望能帮助到你
Imports Microsoft.Win32
...
Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", Application.ProductName, Application.ExecutablePath, RegistryValueKind.String)
试试这个代码: -
FileCopy("Name.Ext", Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "\Name.Ext")
这里(名称.Ext): -
Name - Your Application's name.
Ext - The Extension, it's of-course .exe
这是最简单和最好用的。
可能是旧主题,但添加 Imports System.Security.AccessControl.RegistryRights 应该解决 System.Security.SecurityException:“不允许请求的注册表访问。” Christhofer Natalius 说的麻烦