|
-
Dec 2nd, 2011, 10:10 AM
#1
Thread Starter
Junior Member
[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
Last edited by bagwis; Dec 4th, 2011 at 08:06 AM.
-
Dec 2nd, 2011, 10:31 AM
#2
Fanatic Member
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
-
Dec 2nd, 2011, 10:52 AM
#3
Thread Starter
Junior Member
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.
-
Dec 2nd, 2011, 11:09 AM
#4
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.
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Dec 2nd, 2011, 11:37 AM
#5
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
-
Dec 2nd, 2011, 11:52 AM
#6
Fanatic Member
Re: Question in file path
 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
Last edited by TDQWERTY; Dec 2nd, 2011 at 11:55 AM.
-
Dec 2nd, 2011, 12:38 PM
#7
Thread Starter
Junior Member
-
Dec 2nd, 2011, 12:44 PM
#8
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?
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Dec 2nd, 2011, 04:55 PM
#9
Fanatic Member
Re: Question in file path
use try-catch:
Code:
try
...
catch e....
...
end try
-
Dec 2nd, 2011, 06:54 PM
#10
Thread Starter
Junior Member
Re: Question in file path
Try catch solved my problem. Thanks a lot will post here once I'm done with my program.
-
Dec 3rd, 2011, 09:34 AM
#11
Thread Starter
Junior Member
Re: Question in file path
I finished my program, thanks for your response.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|