[RESOLVED]Question in file path
Good day, I'm just new here in vbforums, I need help in my simple project. I'm quite new in vb.net. I am using visual basic 2008, just for fun I am currently creating a project that would compile all my applications' installer. My target output is to create a program similar to a laptop driver installed in a dvd, wherein if you click like for example "Video" the installer will run and will start installing. I have this code here
Code:
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start("E:\Installers\Utilities\CCleaner.exe")
End Sub
This line of code works properly since it runs the installer, now my main problem is how can I set the path to a something like "Universal path" say for example if I would burn this onto a DVD, the tendency is that the drive letter will change once I inserted it to another dvd-rom then obviously an error will occur since E:\Installers\Utilities\CCleaner.exe is not the valid path. How can I make it similar to an HTML shorthand code like by just using "/about.html" as long as it resides to the current directory the page will open. I hope I made myself clear, and hope somebody could help me with this. Looking forward for your replies. Thank you
Re: Question in file path
Welcome to the vbforums!
There you go...this will return the current path of the exe
Code:
Public ReadOnly Property GetAppPath() As String
Get
Dim aName As String
aName = System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0).FullyQualifiedName
If (Microsoft.VisualBasic.Right(CStr(System.IO.Path.GetDirectoryName(aName)), 1) = "\") Then
Return CStr(System.IO.Path.GetDirectoryName(aName))
Else
Return CStr(System.IO.Path.GetDirectoryName(aName)) & "\"
End If
End Get
End Property
Re: Question in file path
Thanks for a quick response, may I ask where will I put this code? I tried to include it on my form1.vb but it gave me an error. :)
Re: Question in file path
Alternatively you could replace this line
Code:
System.Diagnostics.Process.Start("E:\Installers\Utilities\CCleaner.exe")
with
Code:
Dim strExePath As String = Environment.CurrentDirectory & "\CCleaner.exe"
System.Diagnostics.Process.Start(strExePath)
Please check the syntax.
Re: Question in file path
Or try it like this...
' Usage examples...
LaunchFile("Installers\Utilities\CCleaner.exe")
LaunchFile("SomeFile.exe")
Code:
Private Sub LaunchFile(ByVal FileToLaunch As String)
' add exe's root drive to file path
Dim sPath As String = Application.StartupPath.Substring(0, 3) & FileToLaunch
' if file found then start it
If IO.File.Exists(sPath) Then
Process.Start(sPath)
Else
MessageBox.Show("File not found.", "File missing", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Re: Question in file path
Quote:
Originally Posted by
bagwis
Thanks for a quick response, may I ask where will I put this code? I tried to include it on my form1.vb but it gave me an error. :)
You can include the Function after the Class definition.
replace the
Code:
Public ReadOnly Property GetAppPath() As String
with
Code:
Public Function GetAppPath() As String
then use:
Code:
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start(GetAppPath() & "Installers\Utilities\CCleaner.exe")
End Sub
Re: Question in file path
Many thanks to the three of you :) all the codes were working fine. I have few more questions, sorry. I tried to run the program and it works, but how can I get rid of this error
http://i40.tinypic.com/2u6kwsz.png
this happens if I do not continue with the installation of the application that I clicked.Thank you so much. :)
Re: Question in file path
bagwis,
Based on that obscure error message it is impossible to pinpoint where the error occured. If you run in debug mode, you are more likely to find out the line of code that throws this exception.
What is that line?
Re: Question in file path
use try-catch:
Code:
try
...
catch e....
...
end try
Re: Question in file path
Try catch solved my problem. Thanks a lot :) will post here once I'm done with my program.
Re: Question in file path
I finished my program, thanks for your response.