Question Make A Button Open The File Browsed By OpenFileDialog And Save Path In .txt

hyder7nh

Member
Joined
May 20, 2011
Messages
7
Programming Experience
Beginner
Hello There
im trying to make an application which on start ask the user to browse for a .lnk file
Once the lnk file has been browsed,the application is shown and there are two buttons
1.Launch
2.Exit
When The Launch Button Is Clicked,The .lnk File which the user browsed to at application startup should open.
On exit,well it just exits the application!
and the openfiledialog should pop up only for the first time the user ever opens the application.the file path should be stored in a .txt or .ini or registry file so that next time he doesnt have to browse for it again..how would i go about doing this!Here is my code so far!
VB.NET:
Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim res As MsgBoxResult = MsgBox("Are you sure to exit B3 Launcher?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo)

        If res = MsgBoxResult.Yes Then
            Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        openFD.InitialDirectory = "C:\"
        openFD.Title = "Browse To Your Server's Shortcut"
        openFD.Filter = "Server's Shortcut|*.lnk"
        openFD.ShowDialog()

    End Sub

    Private Sub openFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles openFD.FileOk

    End Sub
End Class
Please help me out!
I Would Really Aprreciate If You Posted The Code Here,Im A Mega Noob In This :)
I Really Need This
Thank You!
 
You can use mySettings to store the settings variables rather than writing a textfile or editing the registry if you want?, How to: Add or Remove Application Settings
How to: Access Settings Events
My.Settings Object

On win7 they get stored in appdata of the user, like an example from one of my apps using it is C:\Users\USERNAME\AppData\Local\PUBLISHERNAME\PROGRAMNAME\VERSION\user.config

So add two settings, one that is of string type and one that is of boolean.

I'm calling them FileLocation and FirstRun(Boolean) in the code below. Be sure to add this otherwise this code will not work. Also the settings will be saved once the app closes or if My.Settings.Save() is called


also check out Process.Start Method (System.Diagnostics)


I added a few things to your code, this should work. Hope it helps.


Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim res As MsgBoxResult = MsgBox("Are you sure to exit B3 Launcher?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo)

        If res = MsgBoxResult.Yes Then
            Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'assuming this is your launch button and the computer already knows how to deal with that type of file
        Process.Start(My.Settings.FileLocation)
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.FirstRun Then 'be sure you add these to your settings in your app.config or using the project properties
            firstRun()

            My.Settings.FirstRun = False
        End If
    End Sub

    Private Sub firstRun()
        openFD.InitialDirectory = "C:\"
        openFD.Title = "Browse To Your Server's Shortcut"
        openFD.Filter = "Server's Shortcut|*.lnk"
        openFD.ShowDialog()
    End Sub

    Private Sub openFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles openFD.FileOk
        If openFD.FileName <> "" Then Then
            My.Settings.FileLocation = openFD.FileName 'be sure you add these to your settings in your app.config or using the project properties
        End If
    End Sub
End Class
 
Last edited:
Yeah you can copy and paste, just make sure to add the settings in your project too. See the link i posted for instructions on how to add settings. How to: Add or Remove Application Settings

Sorry i just noticed a mistake in the code i posted. I just modified the previous post. The code should work now as long as you add the settings, let me know if it doesnt.
 
Last edited:
Finally

I Added Those Settings...at first application opened as normal,no filedialog box opened up then i noticed i had set the FirstRun Boolean thingy to false,i set it to true and it works flawlessly...i love you so much for helping me out[No Homo]
 
also i was wondering if you could tell me,how i would make my program add a desktop shortcut to an application once a button is clicked...my program is an autoinstaller and includes a lot of steps and one of the step is adding the shortcut to the desktop,i could not get this done for VB.NET hence i did it with VB6 and linked it with all the other programs
 
yea no problem. :D


check out this link for the shortcut to the desktop using vs.net
Deploying VB .NET Applications

im not too familiar with the deployment area.. i use visual studio 2010 and the Setup Wizard/Project works perfectly for me. If your using the setupwizard from vs.net it should be fairly simple like shown in the link above. in the filesystem setup you just go to the user desktop and then right click and go to create a shortcut.

if you want to do it via code take a look at this link
winforms - how to create shortcut on desktop in vb.net without installer - Stack Overflow

Otherwise i'm not too sure...
Hope that helps, Good luck with your program!
 
then i noticed i had set the FirstRun Boolean thingy to false,i set it to true and it works flawlessly
Sorry about that, i forgot to mention that part about creating the setting variable. Glad you got it to work though :)
 
Actually

Actually i want the shortcut to be added on a button click :)
and can you tell me where that code goes?do i make a new class or something like that?
 
See link

See the link that i posted it, as it turns out the original is actually from this forum.

http://www.vbdotnetforums.com/vb-net-general-discussion/43040-creating-desktop-shortcut.html

Just copy and paste that function into your class, then create a button.click event and call
CreateShortCut("FullLinkDestinationLocation.lnk", "Title of ShortCut")


See the link, it should work fine, just be sure to import the dll file to your project references. I noticed a small syntax error in the link and posted a correction on that thread. It was just missing a closing ")"



Here is an example using the code JuggaloBrotha posted in that link.

Imports IWshRuntimeLibrary
Imports System.IO

Public Class form1
    Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
        CreateShortCut(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "AwesomeApp.lnk"), "TestingCut")
    End Sub

    Private Sub CreateShortCut(ByVal FileName As String, ByVal Title As String)
        Try
            Dim WshShell As New WshShell

            Dim shortCut As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut(FileName), IWshRuntimeLibrary.IWshShortcut)

            ' set the shortcut properties
            With shortCut
                .TargetPath = Application.ExecutablePath
                .WindowStyle = 1I
                .Description = Title
                .WorkingDirectory = Application.StartupPath
                ' the next line gets the first Icon from the executing program
                .IconLocation = Application.ExecutablePath & ", 0"
                .Arguments = String.Empty
                .Save() ' save the shortcut file
            End With
        Catch ex As System.Exception
            MessageBox.Show("Could not create the shortcut")
        End Try
    End Sub
End Class
 
My App Runs Standalone!Should This Be A Problem

in your post,i saw you reffering a dll or something,my app is publised as a standalone.exe
from the debug folder...should this be a problem?i suppose that dll is added from the .NET Framework or something...
forgive my noobish questions im completely dark in these matters :)
 
forgive my noobish questions
No worries :)


i dont have much experience with the deployment aspect... but i do not think it would be a problem. here are some instructions on how to add/remove a reference in VS, give it a shot, i think it should work. I tried it and it seems to work fine for me. If it is not working, try cleaning out the build bin directory and building it again.

How to: Add or Remove References in Visual Studio

also if you are having trouble finding the dll that was mentioned. It should be under COM and it is called "Windows Script Host Object Model" or "Interop.IWshRuntimeLibrary.dll" when you go to add the reference.
 
Back
Top