使我的程序在Windows启动时启动的问题 [英] Problem with making my program start at windows startup

查看:64
本文介绍了使我的程序在Windows启动时启动的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序在Windows启动时无法正常工作.代码如下:

I have problem with making my program works on windows startup. Here''s the code:

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




正如您在程序上方看到的那样,在路径"Software \ Microsoft \ Windows \ CurrentVersion \ Run"上编辑注册表,并将其成功插入路径中.但是问题是我的程序在Windows启动时无法正常工作. s错了吗???

注意:我的操作系统是Windows 7 32位.




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.

推荐答案

我不希望操纵注册表,而是希望在启动路径中创建快捷方式.您可以在Windows脚本宿主COM库的帮助下进行此操作.

1.在Visual Studio的"COM"选项卡中添加参考:Windows脚本宿主对象模型
2.添加到您的代码中:
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:
Imports IWshRuntimeLibrary


3.实现一个功能,该功能在启动目录中创建一个重定向到您的应用程序的快捷方式:


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

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 ;)


首先,只有管理员才能在HKEY_LOCAL_MACHINE下写入任何内容,因此该代码不适用于普通用户.

其次,运行键中指定的任何内容都在用户登录时运行,而不是Windows启动时运行.如果您想让Windows启动时运行某种东西,则必须编写Windows Service应用程序,而不是Windows Forms.

哦,服务永远都不会建立任何类型的用户界面.
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.


您还可以使用可以从服务中加载应用的任何应用,其中有
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!


这篇关于使我的程序在Windows启动时启动的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆