Results 1 to 11 of 11

Thread: [RESOLVED]Question in file path

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    18

    Resolved [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.

  2. #2
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    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
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    18

    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.

  4. #4
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    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

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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

  6. #6
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Question in file path

    Quote Originally Posted by bagwis View Post
    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.
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    18

    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

    this happens if I do not continue with the installation of the application that I clicked.Thank you so much.

  8. #8
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    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

  9. #9
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Question in file path

    use try-catch:

    Code:
    try
    ...
    catch e....
    ...
    end try
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    18

    Re: Question in file path

    Try catch solved my problem. Thanks a lot will post here once I'm done with my program.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    18

    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
  •  



Click Here to Expand Forum to Full Width