Acess to the path is denied vb.net - 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)

Related

Get fileName into Textbox before Loading Form1

I have a problem with Drag and Drop file Code, I try many methods but I failed this is my Code.
Module Module1
Sub Main(ByVal args() As String)
Dim pathstring As String
If args.Length > 0 Then
Dim path = args(0)
pathstring = path
End If
End Sub
End Module
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = pathstring
End Sub
End Class
Above Code working fine with Console Application, but not in WindowsApplication
I want to get filename into Textbox1 Control before loading Form.
You need to learn how WinForms apps work. That Main method isn't even being executed because it's not the entry point for the app. Unless you disable the Application Framework, you need to handle the app's Startup event if you want to do something before creating the startup form. That said, you can get the commandline arguments anywhere, any time by calling Environment.GetCommandLineArgs.

Getting an access denied error when trying to pause/resume an outgoing message queue

Imports System.Messaging
Imports System.Collections
Imports MSMQ
Imports System.IO
Imports System
Imports System.Messaging.MessageQueue
Imports System.Runtime.InteropServices
Public Class PauseOutMessages
'Declare everything to be in the scope of all methods.
Dim mgmt As New MSMQManagement
Dim outqmgmt As MSMQOutgoingQueueManagement
Dim q As New MSMQApplication
Dim outgoingQueues As New ArrayList
Dim myQueue As New MessageQueue("FormatName:DIRECT=OS:myMachine\Private$\myQueue", QueueAccessMode.ReceiveAndAdmin)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each queue In q.ActiveQueues
If queue.IndexOf("DIRECT=") >= 0 Then
outgoingQueues.Add(queue)
End If
Next
End Sub
Private Sub Pause_Click(sender As Object, e As EventArgs) Handles Pause.Click
For Each queuePath In outgoingQueues
mgmt.Init(FormatName:=queuePath)
outqmgmt = mgmt.Pause()
Next
End Sub
Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
For Each queuePath In outgoingQueues
mgmt.Init(FormatName:=queuePath)
outqmgmt = mgmt.Resume()
Next
End Sub
Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
myQueue.Send("Test")
For Each queue In q.ActiveQueues
If queue.IndexOf("DIRECT=") >= 0 Then
outgoingQueues.Add(queue)
End If
Next
End Sub
End Class
Here is the code I am using, by sending the test message to a non-existing path it gets stuck in the outgoing queue where I want to be able to call MSMQOutgoingQueueManagement.Pause or .Resume to be able to start and stop all outgoing queues.
However I keep getting an error on either mgmt.Pause() or mgmt.Resume() saying Access is denied. I can't seem to find a way to get on to the properties of outgoing queues to be able to adjust security settings. Any help would be greatly appreciated!
SOLVED!
Turns out I just needed to start up visual studio as an administrator and then it worked.

How can i execute an unhide command using cmd on a drive that the users select in visual studio windows forms

I am developing an application that unhides folders and files that were hidden by a virus in windows PC's using visual studio 2012. And I'm developing it using WindowsFormsApplication. So my question is after i have received the users preferred drive using FolderBrowserDialog, how can i make my application execute an unhide command using cmd.exe like "attrib -s -h /s /d ." on the drive that the user selects?
Its not much but, here is what i'hv done so far:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
Label1.Text = command
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
End Class
So when the user clicks Button_2 i want the application to execute a cmd command to unhide files and folders on the selected drive. Any idea how i can achieve that?
Am # the very beginners level, so detailed & simplified answers will be appreciated. Thanks all.
This is from MSDN
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
' Create the file if it does not exist.
If File.Exists(path) = False Then
File.Create(path)
End If
Dim attributes As FileAttributes
attributes = File.GetAttributes(path)
If (attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
' Show the file.
attributes = RemoveAttribute(attributes, FileAttributes.Hidden)
File.SetAttributes(path, attributes)
Console.WriteLine("The {0} file is no longer hidden.", path)
Else
' Hide the file.
File.SetAttributes(path, File.GetAttributes(path) Or FileAttributes.Hidden)
Console.WriteLine("The {0} file is now hidden.", path)
End If
End Sub
Public Shared Function RemoveAttribute(ByVal attributes As FileAttributes, ByVal attributesToRemove As FileAttributes) As FileAttributes
Return attributes And (Not attributesToRemove)
End Function
End Class
You have to change it to do what you want, and do search all the folders and subfolders but it should be a good start.

System.Threading.Timer not firing, global.aspx

I am a newbie, but I am unable to get this code working. FileSweeper is supposed to start a timmer that triggers fileCopy on a web server. fileSweeoer is triggered by global.asax. FileCopy then will copy the file. However the FC.copy never fires. Any help/explanation would be helpful!
Would it make any difference if this was a class running on a web server?
My code is from http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx.
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading
Public Class fileSweeper
Dim stateTimer As Timer
<MTAThread()> _
Sub Main()
Dim FC As New fileCopy
Dim tcb As TimerCallback = AddressOf FC.Copy
stateTimer = New Timer(tcb, "", 20000, 200000)
GC.KeepAlive(stateTimer)
End Sub
End Class
Global.asax:
<%# Application Language="VB" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
Dim FS As New fileSweeper
FS.Main()
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
End Sub
</script>
You are probably going to get unexpected results here because you are likely going to get multiple sessions on a web server. You might want to consider creating a console app and using windows task scheduler.

How to make my program run at startup?

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