Autostart

  • VB.NET

Es gibt 29 Antworten in diesem Thema. Der letzte Beitrag () ist von Blackbird88.

    Hallo

    Ich brauche Hilfe und zwar:

    ich möchte, dass sich mein Programm durch klick eines buttons, von selbst in die registy einträgt, so das sich mein programm beim nächsten system start öffnet.

    Kann mir da Jemand weiterhelfen ?

    Lg
    msdn.microsoft.com/de-de/library/bb979300.aspx
    codeproject.com/KB/dotnet/csregistry01.aspx

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---
    Erstmal Danke

    habe folgenden code




    VB.NET-Quellcode

    1. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
    2. Alias "RegOpenKeyExA" ( _
    3. ByVal hKey As Long, _
    4. ByVal lpSubKey As String, _
    5. ByVal ulOptions As Long, _
    6. ByVal samDesired As Long, _
    7. phkResult As Long) As Long
    8. Private Declare Function RegCloseKey Lib "advapi32.dll" ( _
    9. ByVal hKey As Long) As Long
    10. Private Declare Function RegSetValueEx_String Lib "advapi32.dll" _
    11. Alias "RegSetValueExA" ( _
    12. ByVal hKey As Long, _
    13. ByVal lpValueName As String, _
    14. ByVal Reserved As Long, _
    15. ByVal dwType As Long, _
    16. ByVal lpData As String, _
    17. ByVal cbData As Long) As Long
    18. Private Declare Function RegDeleteValue Lib "advapi32.dll" _
    19. Alias "RegDeleteValueA" ( _
    20. ByVal hKey As Long, _
    21. ByVal lpValueName As String) As Long
    22. Public Const HKEY_CURRENT_USER = &H80000001
    23. Public Const HKEY_LOCAL_MACHINE = &H80000002
    24. Const KEY_QUERY_VALUE = &H1
    25. Const KEY_SET_VALUE = &H2
    26. Const KEY_CREATE_SUB_KEY = &H4
    27. Const KEY_ENUMERATE_SUB_KEYS = &H8
    28. Const KEY_NOTIFY = &H10
    29. Const KEY_CREATE_LINK = &H20
    30. Const KEY_ALL_ACCESS = KEY_QUERY_VALUE Or KEY_SET_VALUE _
    31. Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or _
    32. KEY_NOTIFY Or KEY_CREATE_LINK
    33. Const ERROR_SUCCESS = 0&
    34. Const REG_NONE = 0
    35. Const REG_SZ = 1
    36. ' Anwendung in Registry eintragen
    37. '
    38. ' sDescr: Beschreibung/Bezeichnung
    39. ' sPath : Pfad inkl. Dateiname der Anwendung
    40. ' ===========================================
    41. Public Function SetAutoRun(sDescr As String, _
    42. sPath As String) As Boolean
    43. Dim lResult As Long
    44. Dim KeyHandle As Long
    45. Dim Key As String
    46. Const root = HKEY_CURRENT_USER
    47. Key = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
    48. lResult = RegOpenKeyEx(root, Key, 0, KEY_ALL_ACCESS, _
    49. KeyHandle)
    50. If lResult <> ERROR_SUCCESS Then
    51. SetAutoRun = False
    52. Exit Function
    53. End If
    54. ' +1 für die Null am Ende
    55. lResult = RegSetValueEx_String(KeyHandle, sDescr, 0, _
    56. REG_SZ, sPath, Len(sPath) + 1)
    57. RegCloseKey KeyHandle
    58. SetAutoRun = (lResult = ERROR_SUCCESS)
    59. End Function
    60. ' aus Registry entfernen
    61. '
    62. ' sDescr: Beschreibung/Bezeichnung
    63. ' =================================
    64. Function RemoveAutoRun(sDescr As String) As Boolean
    65. Dim lResult As Long
    66. Dim KeyHandle As Long
    67. Dim Key As String
    68. Const root = HKEY_CURRENT_USER
    69. Key = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
    70. lResult = RegOpenKeyEx(root, Key, 0, _
    71. KEY_ALL_ACCESS, KeyHandle)
    72. If lResult <> ERROR_SUCCESS Then
    73. RemoveAutoRun = False
    74. Exit Function
    75. End If
    76. lResult = RegDeleteValue(KeyHandle, sDescr)
    77. RemoveAutoRun = (lResult = ERROR_SUCCESS)
    78. RegCloseKey KeyHandle
    79. End Function
    80. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    81. If SetAutoRun("Kran iff", "c:\Users\Blackbird\Desktop\VISUAL BASIC\Neuer Ordner\Übungen\Kran iff\Kran if\bin\Debug\Kran iff.exe") Then
    82. MsgBox("Eintrag war erfolgreich.")
    83. Else
    84. MsgBox("Eintrag war nicht erfolgreich.")
    85. End If
    86. End Sub
    87. End Class



    wenn ich jetzt auf Button1 klicke bekomm ich die meldung MsgBox("Eintrag war nicht erfolgreich.") warum ?
    Hi,

    das geht viel einfacher, siehe:

    VB.NET-Quellcode

    1. If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", Application.ProductName, Application.ExecutablePath) IsNot Nothing Then
    2. My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", Application.ProductName, Application.ExecutablePath)
    3. End If


    Ciao...

    VB.NET-Quellcode

    1. Imports System.Security
    2. Public Class Form1
    3. Public password As SecureString = New SecureString()
    4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5. Me.txtUser.Text = System.Environment.UserName
    6. End Sub
    7. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8. StartNewProcessWithAdminCredentials("C:\MeineAnwendung", Me.txtUser.Text)
    9. If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "kran iff", "Kran iif") IsNot Nothing Then
    10. My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "kran iff", "Kran iif")
    11. End If
    12. End Sub
    13. Private Function StartNewProcessWithAdminCredentials(ByVal ExecutablePathAndName As String, _
    14. ByVal sUsername As String, _
    15. Optional ByVal StartArguments As String = "") As System.Diagnostics.Process
    16. Dim newProcessStartUpInfo As System.Diagnostics.ProcessStartInfo
    17. Dim ReturnCode As Boolean = False
    18. Try
    19. newProcessStartUpInfo = New System.Diagnostics.ProcessStartInfo(ExecutablePathAndName, StartArguments)
    20. 'XP oder hoeher (Vista = 6)
    21. If System.Environment.OSVersion.Version.Major >= 5 Then
    22. newProcessStartUpInfo.Verb = "runas"
    23. newProcessStartUpInfo.UseShellExecute = False
    24. newProcessStartUpInfo.Password = password
    25. newProcessStartUpInfo.UserName = sUsername
    26. Return System.Diagnostics.Process.Start(newProcessStartUpInfo)
    27. Else
    28. Return System.Diagnostics.Process.Start(newProcessStartUpInfo)
    29. End If
    30. Catch ex As Exception
    31. MsgBox(ex.Message)
    32. Return Nothing
    33. End Try
    34. End Function
    35. Private Sub txtPass_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPass.Enter
    36. password.Clear()
    37. End Sub
    38. Private Sub txtPass_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPass.KeyPress
    39. Select Case Asc(e.KeyChar)
    40. Case 33 To 126
    41. password.AppendChar(ChrW(Asc(e.KeyChar)))
    42. Case Else
    43. e.Handled = True
    44. End Select
    45. End Sub
    46. End Class


    So hab ich es probiert
    ja ich bin als administrator angemdet

    Stellen Sie sicher ,dass sie über ausreichende Berechtigungen für den Zugriff auf diese Ressource verfügen

    Stellen Sie beim zugreifen auf eine Datei sicher,dass diese nicht schreibgeschütz ist . " Ist sie nicht "
    Probiers mal mit dem Code(autostart zu erstellen)

    VB.NET-Quellcode

    1. Dim key As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Run")
    2. key.SetValue(My.Application.Info.Title, """" & System.Reflection.Assembly.GetEntryAssembly.Location & """")

    bei mir geht er. (Win7 32 Bit)
    @Blackbird88
    muss man da nicht den schlüssel HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run nehmen?

    lg Gugi

    edit by kevin89: ein volles Quote ist hier nicht notwendig!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „kevin89“ ()

    jvbsl schrieb:

    beides sind(wie von mir bereits gepostet) richtige Schlüssel, das eine ist jedoch für den aktuellen Benutzer und das andere für alle ;)

    muss man nicht wenn man nicht wenn man den schlüssel HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run verwendet, unter projekt -> eigenschaften -> einstellungen für die benutzerkontensteuerung, die zeile

    Quellcode

    1. <requestedExecutionLevel level="asInvoker" uiAccess="false" />

    durch

    Quellcode

    1. <requestedExecutionLevel level="highestAvailable" uiAccess="false" />

    ersetzen?
    bei dem HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run muss man ja nur

    Quellcode

    1. <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

    schreiben, oder?

    lg Gugi