How to make my program run at startup? - vb.net

I'm programming a desktop application similar to Google desktop but with my own gadget with vb.net 2008 how can i make my application when the user install it on their computer to run at the time of start up?
Let assume that my application name is windowsAplication1 and I'm using windows XP and the program will be installed on C drive?

You can add it to registry with the following code
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
you can remove it using
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)
The above code will add it to all users. You can add it to current user in the following key
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Or you can add a link to your application in the "Startup" folder.
I suggest you dont do it automatically it may irritate the user. I hate when apps add them automatically to Windows Startup. Give an option for the user to run the program on windows startup.

Simpley use this code:
Dim info As New FileInfo(application.startuppath)
info.CopyTo(My.Computer.FileSystem.SpecialDirectories.Programs + "\startup\myapp.exe")
hope it helps.

You Can Do this Using the code below
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This is where you'll need to have the program
' set the check box to the previous selection that
' the user has set. It's up to you how you do this.
' For now, I'll set it as "unchecked".
CheckBox1.Checked = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' The following code is a rendition of one provided by
' Firestarter_75, so he gets the credit here:
Dim applicationName As String = Application.ProductName
Dim applicationPath As String = Application.ExecutablePath
If CheckBox1.Checked Then
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.SetValue(applicationName, """" & applicationPath & """")
regKey.Close()
Else
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.DeleteValue(applicationName, False)
regKey.Close()
End If
' Assuming that you'll run this as a setup form of some sort
' and call it using .ShowDialog, simply close this form to
' return to the main program
Close()
End Sub

Imports Microsoft.Win32
...
Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", Application.ProductName, Application.ExecutablePath, RegistryValueKind.String)

A simple method
Imports Microsoft.Win32
...
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run",True)
regKey.SetValue(Application.ProductName, Application.ExecutablePath)
regKey.Close()
Hope it helps

Just try this code :-
FileCopy("Name.Ext", Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "\Name.Ext")
Here (Name.Ext) :-
Name - Your Application's name.
Ext - The Extension, it's of-course .exe
It's the simplest and best to use.

Might be old topic but adding
Imports System.Security.AccessControl.RegistryRights
Should resolve System.Security.SecurityException: 'Requested registry access is not allowed.' trouble stated by Christhofer Natalius

Related

Run program without form on windows startup

What basically i want is to show is:
notifyicon.visible = true
I mean to show a tray icon when the windows starts up but the program form should not be shown ,how could i achieve it ?
I got to know that by adding to registry you can run the program on startup example below
Dim regkey As RegistryKey
regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", True)
If (runonstartupToolStripMenuItem.Checked = True) Then
' Add the value in the registry so that the application runs at startup
regkey.SetValue("Your Application Name", Application.ExecutablePath.ToString())
Else
' Remove the value from the registry so that the application doesn't start
regkey.DeleteValue("Your Application Name", False)
End If
but this will run the whole program and will make form show up which i do not want unless user manually starts it.
Add this code to your form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide() ' <= Required
Me.ShowInTaskbar = False ' <= Required
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(10000)
End Sub
When the program opens when the windows starts it should open with a unique parameter,
And when the unique parameter is found the form will be hidden,
Conversely if the user opens the program, will not have the parameter and then the form can show.
I am use checkbox to set and unset:
Private Sub cbStartup_CheckedChanged(sender As Object, e As EventArgs) Handles cbStartup.CheckedChanged
Dim applicationName As String = Application.ProductName
Dim applicationPath As String = Application.ExecutablePath
If cbStartup.Checked = True Then
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.SetValue(applicationName, """" & applicationPath & """")
regKey.Close()
Else
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.DeleteValue(applicationName, False)
regKey.Close()
End If
End Sub
Test working on VS 2015 run on Windows 10 64Bit.

Acess to the path is denied vb.net

Why I'm i seeing this message. I just wanted to add the application to start each time with windows and then it popups me this message.
Access to the path .... is denied.
I add to startup with this code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim info As New FileInfo(Application.StartupPath)
info.CopyTo(My.Computer.FileSystem.SpecialDirectories.Programs + "\startup\Apps.exe")
End Sub
If you want that your application to start on windows startup you can use the registry.
To add an application to the system use this code -
Imports Microsoft.Win32
Public Sub RunAtStartup(ByVal ApplicationName As String, ByVal ApplicationPath As String)
Dim CU As Microsoft.Win32.RegistryKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
With CU
.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
.SetValue(ApplicationName, ApplicationPath)
End With
End Sub
And to use the function just do -
RunAtStartup(Application.ProductName,Application.ExecutablePath)

VB.NET 2008 - Input to data to website controls and download results

this is my first Q on this website so let me know if I have missed any important details, and thanks in advance.
I have been asked to access a website and download the results from a user-inputted form. The website asks for a username/password and once accepted, several questions which are used to generate several answers.
Since I am unfamiliar with this area I have set up a simple windows form to tinker around with websites and try to pick things up. I have used a webbrowser control and a button to use it to view the website in question.
When I try to view the website through the control, I just get script errors and nothing loads up. I am guessing I am missing certain plug-ins on my form that IE can handle without errors. Is there anyway I can identify what these are and figure out what to do next? I am stumped.
The script errors are:
"Expected identifier, string or number" and
"The value of the property 'setsection' is null or undefined"
Both ask if I want to continue running scripts on the page. But it works in IE and I cannot see why my control is so different. It actually request a username and password which works fine, it is the next step that errors.
I can provide screenies or an extract from the website source html if needed.
Thanks,
Fwiw my code is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("http://website.com")
'WebBrowser1.Navigate("http://www.google.com")
End Sub
Thanks for Noseratio I have managed to get somewhere with this.
Even though the errors I was getting seemed to be related to some XML/Java/Whatever functionality going askew it was actually because my webbrowser control was using ie 7.0
I forced it into using ie 9 and all is now well. So, using my above example I basically did something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'WebBrowser1.ScriptErrorsSuppressed = True
BrowserUpdate()
WebBrowser1.Navigate("http://website.com")
'WebBrowser1.Navigate("http://www.google.com")
End Sub
Sub BrowserUpdate()
Try
Dim IEVAlue As String = 9000 ' can be: 9999 , 9000, 8888, 8000, 7000
Dim targetApplication As String = Process.GetCurrentProcess.ToString & ".exe"
Dim localMachine As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim parentKeyLocation As String = "SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl"
Dim keyName As String = "FEATURE_BROWSER_EMULATION"
Dim subKey As Microsoft.Win32.RegistryKey = localMachine.CreateSubKey(parentKeyLocation & "\" & keyName)
subKey.SetValue(targetApplication, IEVAlue, Microsoft.Win32.RegistryValueKind.DWord)
Catch ex As Exception
'Blah blah here
End Try
End Sub

How to hide Windows 7 Open File Security when running certain EXE file using VB.NET?

Hello dearest community,
I am trying to build a simple AutoUpdate application using VB.NET. It was quite simple. That is, I put the newest ZIP file in my hosting site, and then download it using WebClient.DownloadFileAsync. After it get downloaded, I extract it using http://stahlforce.com/dev/unzip.exe
But each time I run the unzip.exe using Process.start, Windows 7 always show Open File Security.
Is it possible for VB.NET to bypass such security restriction?
Thanks.
Btw, this is my code of using WebClient.DownloadFileAsync, in case any one google about it and landed on this page :
Public Class AutoUpdate
Dim installationFolder As String = "C:\Program Files\xyz\abc\"
Dim updateFileNameTarget As String
Private Sub btnStartUpdte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartUpdte.Click
lblPercent.Text = ""
lblDownloading.Text = ""
lblDownloading.Text = ""
pbDownloadStatus.Value = 0
Dim wc As New WebClient
AddHandler wc.DownloadFileCompleted, AddressOf downloadComplete
AddHandler wc.DownloadProgressChanged, AddressOf progressChanged
Dim path As String = "http://xyz.abc.com/test.zip"
updateFileNameTarget = installationFolder & "test.zip"
Try
If File.Exists(updateFileNameTarget) Then
File.Delete(updateFileNameTarget)
End If
lblDownloading.Text = "Downloading " & path
wc.DownloadFileAsync(New Uri(path), updateFileNameTarget)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub progressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
pbDownloadStatus.Value = e.ProgressPercentage
lblPercent.Text = e.ProgressPercentage & "%"
End Sub
Private Sub downloadComplete(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
MessageBox.Show("Download complete. Now extracting")
Dim cmd As String = Application.StartupPath & "\Tools\unzip.exe"
Dim arg As String = "-o """ & updateFileNameTarget & """"
Process.Start(cmd, arg)
End Sub
End Class
If you're already process-invoking everything else (including unzip), also use Sysinternal's streams.exe. Use the -d flag to remove the NTFS alternate data streams (ADS). There should only be one - and it is the one that indicates to Windows that the file was downloaded from an "untrusted source".
Your downloaded files will currently have a stream that looks like this:
:Zone.Identifier:$DATA 26
Remove this stream from the download files after extracting but before execution, and the warning will no longer appear.
See also: What is Zone Identifier? - and Accessing alternate data streams in files for a library to work with these within .NET without needing streams.exe.

How do I discover the user's Desktop folder?

I'm making a little application in visual studio which loads a ROM in an emulator.
I have two emulators and 20 ROMs.
I made a form and added a few buttons. When you click the Button it opens a new form and closes the old one. Then on the new form I have four buttons: each one loads a different ROM in an emulator. So when you press Button1 this code is triggered:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Shell("C:\Users\shifty\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe ""C:\Users\shifty\Desktop\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus)
End Sub
It works fine - I click it and it loads the game in the emulator. The bit im having trouble with is the file paths. If I send this application to a friend, it would still look for "C:\Users\shifty\Desktop\" - but that's on my computer, not his.
Is there a way to make the application look for the file on his computer (without changing the file path to (C:\Users\""his user name""\Desktop))
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
This will resolve to be the desktop folder for the current user.
It will even work between XP, vista and Windows 7 properly.
Old post but I have to side with Mc Shifty. You can't assume that everyone is a coding expert. If they were then they wouldn't be here asking questions like that.
None of the answers given above were complete
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) <<< includes and extra )
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); <<< extra ) and the ; is C or java not VB which he is obviously using by his example code.
Both of those only give you half of the required code to generate something usable.
Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
The above code will give you the result needed, c:\users\shifty\desktop
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Shell(s & "\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe " & s & "\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus)
End Sub
There's a mechanism to get the current user's Desktop directory, using Environment.SpecialFolder.
Usage:
Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
I had problems using the Environment.GetFolderPath method from previous answers.
The following works in VB 2012, My.Computer.FileSystem.SpecialDirectories.Desktop
So, if you have a file on a users desktop named "contacts.txt", the following will display the full path,
' Desktop path
Dim desktopPath = My.Computer.FileSystem.SpecialDirectories.Desktop
' Concatenate desktop path and file name
filePath = desktopPath & "/contacts.txt"
MsgBox(filePath)
Documentation
Really old post at this point, but hey, found what I was looking for.
MC SH1FTY, I assume you have figured this out already, but to do what you are trying to do:
1) Call in that code that Spence wrote as a variable (I'd declare it Globally, but that's my preference. To do that:
Public userDesktopLoc As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
2) Either use this DIRECTLY in your code, or make another string to concatenate a directory:
Option A)
Public emulatorPath As String = userDesktopLoc & "pokemon games\Emulator\VBA\VisualBoyAdvance.exe "
Public romPath As String = userDesktopLoc & "pokemon games\Roms\Yellow\Pokemon Yellow.gb"
Then, within your Subroutine, replace your current Shell statement with:
Shell(emulatorPath & romPath, vbNormalFocus)
Or, Option B, which is thedsz's answer:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Shell(s & "\Desktop\pokemon games\Emulator\VBA\VisualBoyAdvance.exe " & s & "\pokemon games\Roms\Yellow\Pokemon Yellow.gb""", vbNormalFocus)
End Sub
By using that you guarantee that the emulator is on the users desktop. This is not always the case. I know I move things around that I download or a friend sends to me. It's better to use App.Path and make sure your emulator.exe is in the directory with your little front end program (usually the case).
the answer is simple.
put this at the top of the form
"Public thepath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)"
that ensures that the file is on their desktop!
then" click on your button or whatever you used to open the emu and type
"Process.Start(thepath + "the emulator.exe "+ "the rom you want")
You need to use a file open dialog to choose your path for the two files. Here is an example.
You then use the two paths in your code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yellow.Click
Shell(emulatorPath + "\"" + romPath + "\"", vbNormalFocus)
End Sub