wpfvb.netcopystartupstartup-folder

WPF Vb.net Copyto not working?


I am simply tyring to fire an event that copies a program to the Startup folder. I do not understand where I am going wrong? I keep on getting exception message. The file that is being copies is NOT in use.

 Try
        Dim DesktopLink As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        Dim StartupFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)

 Dim info As New FileInfo(StartupFolder)
            info.CopyTo(DesktopLink + "\doessomething.bat")

    Catch ex As Exception
        MessageBox.Show("Error: Can not copy to startup folder")
    End Try

Solution

  • Right now, you're creating a FileInfo from a folder, not a file.

    This should likely be:

    Dim info As New FileInfo(Path.Combine(StartupFolder, "doessomething.bat"))
    info.CopyTo(Path.Combine(DesktopLink, "doessomething.bat"))
    

    Or, even easier:

    Dim source = Path.Combine(StartupFolder, "doessomething.bat")
    Dim target = Path.Combine(DesktopLink, "doessomething.bat")
    File.Copy(source, target)