We help IT Professionals succeed at work.
Get Started

Start App with Windows 7 all profiles vs individual profile

107 Views
Last Modified: 2015-10-02
Hello,

I'm working on an app that will start with Windows 7 log on.  Each PC has about 30 users.  Because I'm lazy and I've had some permission problems with a different app writing to the registry for this I just set the installer to create a link in the all user startup folder.  I'd like to give individual users the option to not have it start up with their account only but leave everyone else's alone.  I use the following code to handle an entry in the users startup folder but then I get two entries in that users startup folder and the program starts up in a window instead of minimized to system tray like it should.  I've set the app to individual instance.  My suspicions are that I'll have to manage the start up link in individual profiles at time of installation (and somehow add it to users as they are added to that PC).  Is there an easy (lazy) way to manage this idea?  I don't necessarily need to allow users the ability to not have it startup, was just an optional feature.

 Private Sub chkStartWithWindows_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkStartWithWindows.CheckedChanged
        If Not FormLoaded Then Exit Sub
        My.Settings.StartWithWindows = Me.chkStartWithWindows.CheckState
        My.Settings.Save()

        Try
            If Me.chkStartWithWindows.Checked Then
                 AddShortcut(Nothing, Nothing)
            Else
                DeleteStartupFolderShortcuts(Application.ExecutablePath)
            End If
        Catch ex As Exception
            LogIt("ERROR putting startup startup path into startup folder." & ex.ToString)
        End Try
    End Sub

 Private Sub AddShortcut(sender As System.Object, e As System.EventArgs)
        Dim wshShell As New WshShellClass()
        Dim shortcut As IWshRuntimeLibrary.IWshShortcut
        Dim startUpFolderPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)

        ' Create the shortcut
        Try
             'Try to remove existing if its there
            DeleteStartupFolderShortcuts(Application.ExecutablePath)

            shortcut = DirectCast(wshShell.CreateShortcut((startUpFolderPath & Convert.ToString("\")) + Application.ProductName + ".lnk"), IWshRuntimeLibrary.IWshShortcut)

            shortcut.TargetPath = Application.ExecutablePath
            shortcut.WorkingDirectory = Application.StartupPath
            shortcut.Description = "Launch MyAppat startup"
            shortcut.IconLocation = Application.StartupPath + "\MyApp.ico"
            shortcut.Save()
        Catch ex As Exception
            LogIt("Error creating a startup folder entry.   " & ex.ToString)
        End Try
    End Sub

    Public Sub DeleteStartupFolderShortcuts(targetExeName As String)
        Dim startUpFolderPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
        Dim di As New DirectoryInfo(startUpFolderPath)
        Dim files As FileInfo() = di.GetFiles("*.lnk")      
        For Each fi As FileInfo In files
            Dim shortcutTargetFile As String = GetShortcutTargetFile(fi.FullName)

            If shortcutTargetFile.EndsWith(targetExeName, StringComparison.InvariantCultureIgnoreCase) Then
                System.IO.File.Delete(fi.FullName)
            End If
        Next
    End Sub
Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE