Skip to content

Instantly share code, notes, and snippets.

@sedrubal
Created September 29, 2013 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sedrubal/6757090 to your computer and use it in GitHub Desktop.
Save sedrubal/6757090 to your computer and use it in GitHub Desktop.
a vb.net module, to register or unregister the current program in system-startup-entries.
Option Strict On
'TODO Add an COM-reference to C:\Windows\System32\shell32.dll
''' <summary>
''' a module, to register or unregister the current project in system-startup-entries
''' </summary>
''' <remarks>Add an COM-reference to C:\Windows\System32\shell32.dll</remarks>
Module StartUpHandler
''' <summary>
''' Requests, if the current programm is registered in the system-startup-entries, or registers it. CAUTON: Startup entries can be disabled with special programs under Windows8 with the task manager or under other OS with other programs without being registered by the program!
''' </summary>
''' <value>True, if the current programm should start on systemstartup, otherwise False. CAUTON: To check, if this action (setting the value) had been successful, you should request, if isStartUp equals the required value!</value>
''' <returns>True, if the current programm automatically starts on systemstartup, otherwise False.</returns>
Public Property IsStartUp As Boolean
Get
Return My.Computer.FileSystem.FileExists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "\" & Application.ProductName & ".lnk")
End Get
Set(value As Boolean)
If value Then
CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "\" & Application.ProductName & ".lnk", Application.ExecutablePath, , My.Application.Info.Description)
Else
My.Computer.FileSystem.DeleteFile(Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "\" & Application.ProductName & ".lnk")
End If
End Set
End Property
''' <summary>
''' Creates a link
''' </summary>
''' <param name="sLinkFile">Name location of the link-file (.lnk)</param>
''' <param name="sTargetFile">Path+name of the application, that should run by the link</param>
''' <param name="sArguments">Arguments for the start of the program</param>
''' <param name="sDescription">Description for the application</param>
''' <param name="sWorkingDir">Path, in wich the app should run (workingdirectory)</param>
''' <returns>True, if the Action was successful</returns>
Public Function CreateShortcut(ByVal sLinkFile As String, ByVal sTargetFile As String, Optional ByVal sArguments As String = "", Optional ByVal sDescription As String = "", Optional ByVal sWorkingDir As String = "") As Boolean
Try
Dim oShell As New Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oLink As Shell32.ShellLinkObject
'find out the Directory and the filename
Dim sPath As String = sLinkFile.Substring(0, sLinkFile.LastIndexOf("\"))
Dim sFile As String = sLinkFile.Substring(sLinkFile.LastIndexOf("\") + 1)
'Important! create Link-File (0 Bytes)
Dim F As Short = CShort(FreeFile())
FileOpen(F, sLinkFile, OpenMode.Output)
FileClose(F)
oFolder = oShell.NameSpace(sPath)
oLink = CType(oFolder.Items.Item(sFile).GetLink, Shell32.ShellLinkObject)
'Properties of the link-file
With oLink
If sArguments.Length > 0 Then .Arguments = sArguments
If sDescription.Length > 0 Then .Description = sDescription
If sWorkingDir.Length > 0 Then .WorkingDirectory = sWorkingDir
.Path = sTargetFile
'save link
.Save()
End With
'destroy objects
oLink = Nothing
oFolder = Nothing
oShell = Nothing
Return True
Catch ex As Exception
'Error! If the linkfile exists, then it must be deleted
If System.IO.File.Exists(sLinkFile) Then Kill(sLinkFile)
Return False
End Try
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment