Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
I have problem with making my program works on windows startup. Here's the code:
VB
Sub RunAtStartup(ByVal PutAtStartup As Boolean, ByVal Name As String, ByVal Path As String)
        Dim StartupPath As String = "Software\Microsoft\Windows\CurrentVersion\Run"
        Dim myKey As Microsoft.Win32.RegistryKey
        myKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(StartupPath, True)
        If PutAtStartup = True Then
            Try
                myKey.SetValue(Name, Path)
            Catch ex As Exception
                ErrLog &= ex.ToString & vbNewLine
            End Try
        Else
            Try
                myKey.DeleteValue(Name, False)
            Catch ex As Exception
                ErrLog &= ex.ToString & vbNewLine
            End Try
        End If
    End Sub



As you see above my program edit the registry on path "Software\Microsoft\Windows\CurrentVersion\Run"" and the value it succefully inserted in the path. But the problem is my program dosen't works on windows startup what's the wrong?????

Note: my OS is windows 7 32bit.
Posted
Comments
Richard MacCutchan 24-Oct-12 7:28am    
Is your program running with Administrator privilege?
[no name] 24-Oct-12 7:30am    
I already done this. But the problem stay the same.

Instead of manipulating your registry I would prefer to create a shortcut in the startup path. You can do this with the help of the Windows Scripting Host COM Library.

1. Add Reference: Windows Script Host Object Model from COM tab in Visual Studio
2. add to your code:
VB
Imports IWshRuntimeLibrary

3. Implement a function that creates a shortcut in the startup directory redirecting to your application:

VB
Dim WshShell As WshShellClass = New WshShellClass
Dim MyShortcut As IWshRuntimeLibrary.IWshShortcut

' Shortcut will be created in the startup directory
Dim StartupFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
MyShortcut = CType(WshShell.CreateShortcut(StartupFolder & "MyShortcut.lnk"), IWshRuntimeLibrary.IWshShortcut)

' Specify target file full path
MyShortcut.TargetPath = "PATH_TO_YOUR_APP" 

MyShortcut.Save()


Hope this helps! But code is untested ;)
 
Share this answer
 
v2
Comments
[no name] 24-Oct-12 12:25pm    
But there are alot of programs runs on windows startup using registry. This is impossible... there is must way to run it using registry...
First, only administrators can write to anything under HKEY_LOCAL_MACHINE, so this code won't work for normal users.

Second, anything specified in the Run key runs when a user logs in, not when Windows starts up. If you want soemthing to run when Windows starts, you'll have to write a Windows Service application, not Windows Forms.

Oh, and Services do not ever put up any kind of user interface.
 
Share this answer
 
Comments
[no name] 24-Oct-12 12:23pm    
So, if I want to run my program ( which it contains forms ) first run windows service which in turn runs my program that contains forms. But this procedure not quit right.
Is there another ways.
Dave Kreskowiak 24-Oct-12 12:34pm    
A service will NOT launch a program on the users Desktop. That's a security concern and is not allowed.

If your application has visible forms, it can't be a service. You would have to rewrite your application as a service that does its work without any user intervention at all and no UI to show.

If your application requires a user to be there and interact with the app, then it can't be a Service and can NOT be launched when Windows starts.

Since we know nothing of what your application is supposed to be doing, there's little else anyone can say about this.
[no name] 24-Oct-12 13:41pm    
There are alot of programs are running on windows startup like IDM. But my program dosen't works on windows startup. The operation works on windows xp but not on windows 7.
Dave Kreskowiak 24-Oct-12 15:44pm    
There are a lot of Services that start on Windows start. They run independent of a user being logged it. Then, when a user does log in, they get to run an app that communicates with the service and handles all the UI for it.

You seem to think that these are single programs that do everything from service work to UI. They're not. They're multiple applications working together, giving you the illusion that it's a single application.
Dave Kreskowiak 24-Oct-12 15:46pm    
What used to work on XP will no longer work on Windows 7 because of security risks. Under XP, you could get away with having a service put up a UI. That exact same app on Windows 7 will not show the UI on the users desktop. They'll get a notification that a service has put up a message and ask the user if they want to switch to the service desktop to see it. It's a bad idea to have any service attempt to put up a UI.
You can also use any application that would load your app from a service, there was the XYNTService[^] here in CP that could be used for that,

It is easy to use and could do what you want without even loggin in, but of course, it all depends on your real situation here, what your users want to do...

This should be understood as some extra information to Dave's answer...

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900