App.Path in VB.NET


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: App.Path in VB.NET

Hybrid View

  1. #1
    Join Date
    Nov 2004
    Location
    UK
    Posts
    185

    Unhappy App.Path in VB.NET

    Hi all,

    I know VB.NET does not support App.Path, so i found this bit of code:
    Code:
        Public Function App_Path() As String
            Return System.AppDomain.CurrentDomain.BaseDirectory()
        End Function
    its instructions say copy and Unlike app.path in vb6, this includes the the "\" at the end of the value.

    I dont understand what it mean by "\", can anyone advise?
    Thankyou

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    In VB6, App.Path only includes a backslash (\) at the end of the path if the app is running in the root directory of the drive. That is, if the app is running in the root directory of drive C:, App.Path returns "C:\". If, on the other hand, the app is running in C:\Program Files\My App, App.Path returns "C:\Program Files\My App" (no backslash at the end). Because of this discrepancy, you need to check for the presence of a trailing backslash before appending a file name to the path.

    Your VB.NET App_Path function returns a trailing backslash in all cases -- "C:\" or "C:\Program Files\My App\" -- so you may simply append a file name without checking for a backslash.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    The "real" replacement for App.Path is the following:

    Application.ExecutablePath

    VB.NET also offers a second option that returns the working directory at the start of the application:

    Application.StartupPath

    Going through the AppDomain works most of the time, but an AppDomain and an Application are not the same, and in some cases, the code you used could link to something else than the directory you are looking for.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  4. #4
    Join Date
    Apr 2009
    Location
    Germay
    Posts
    1
    The replacement Application.ExecutablePath delivers the path of the executable, but not of the dll. If you need the path of the dll that was called as an addin inside of an application (to read config files or whatever) you have to do it this way:

    Code:
    Module modAppInfos
        Public Function App_Path() As String
            App_Path = CStr(Nothing)
            App_Path = System.Reflection.Assembly.GetExecutingAssembly.GetName().CodeBase
            If StrComp(LCase(Left(App_Path, 8)), "file:///") = 0 Then
                App_Path = Mid(App_Path, 9) 'file:/// entsorgen
            End If
            App_Path = StripFileName(App_Path)
        End Function
    End Module

  5. #5
    Join Date
    Apr 2009
    Posts
    2

    Reply on use of ExecutablePath vs. StartupPath

    Just a quick "Thank You" as this answered my question/solved my problem. However, to provide a bit further info, in VB.NET 2002 at least (net 1.1) I found that Application.ExecutablePath was not useful, as it had not only the path, but included the executable file name as well, which I would have had to strip, however, the other suggestion: Application.StartupPath worked perfectly.
    Thanks

  6. #6
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Glad to help.

    A little trick that, once again, shows that a lot of things are simpler than they might appear at first sight. You can easily strip the file name from a full name to retrieve only the path:

    Code:
    App_Path = New System.IO.FileInfo(Application.ExecutablePath).DirectoryName
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  7. #7
    Join Date
    Apr 2009
    Posts
    2

    Wink

    Again, thank you JBourgeois,
    The technique using ".DirectoryName" was a very nice approach that I was completely unaware of (not suprising as I'm rather new to .NET - I was a "crusty" VB6'er for far too long ~8^D). I don't know how many times I've done the 'ol "instr" searching for the last "\" and fussing the path from a full file spec string.

    Best Regards
    John Andrews

  8. #8
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,262
    Quote Originally Posted by JBourgeois View Post
    Glad to help.

    A little trick that, once again, shows that a lot of things are simpler than they might appear at first sight. You can easily strip the file name from a full name to retrieve only the path:

    Code:
    App_Path = New System.IO.FileInfo(Application.ExecutablePath).DirectoryName
    One quick question! Why are you using 'New' ?
    If FileInfo().DirectoryName returns a string and App_Path is a string then why would you need the 'New' in this statement?

  9. #9
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,262
    Thank you! I will check it out.

Similar Threads

  1. Replies: 6
    Last Post: 11-06-2002, 02:42 PM
  2. Will VB.NET be more stable than VB6?
    By Jason in forum .NET
    Replies: 125
    Last Post: 10-05-2002, 04:34 PM
  3. Is VB.Net difficult or not - OR - How difficult?
    By Michael Culley in forum .NET
    Replies: 6
    Last Post: 06-19-2002, 09:11 AM
  4. array declarations in VB.NET
    By Bill McCarthy in forum .NET
    Replies: 14
    Last Post: 04-10-2001, 05:03 AM
  5. VB.NET: 3 points of view
    By David Kroll in forum .NET
    Replies: 33
    Last Post: 02-13-2001, 10:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center