Results 1 to 4 of 4

Thread: [RESOLVED] How to get the Application Path from DAL layer?

  1. #1

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Resolved [RESOLVED] How to get the Application Path from DAL layer?

    First i'll explain that i want to get the Application Path from DAL layer because i don't want to hardcode the path to my Access Database file, i can use the Settings in DAL dll to store values that won't change like database file name but Path is not always the same, it depends on installation folder. Also, I cannot use Application.StartUpPath because i'm in a DLL, not in the EXE.

    I found this way is close to what i want, but the problem is that this returns the DLL file path, not the Application file path, so if the DLL is in Windows/System32 instead of the Application Path then this won't work, the blue line gets the path:
    Code:
          Dim connectionString As String
          Dim lPath As String = New Uri(Assembly.GetAssembly(Me.GetType).CodeBase).LocalPath
          connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & lPath & _
                        "\base\" & My.Settings.DB_NAME & ";Persist Security Info=False;"
    Last edited by jcis; Jul 1st, 2012 at 01:53 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get the Application Path from DAL layer?

    If you were going to use the Assembly class then you would call Assembly.GetEntryAssembly to get an Assembly object that represents the entry point for the current process, i.e. the EXE. You wouldn't use the Assembly class though. All you need is this:
    vb.net Code:
    1. connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\base\" & My.Settings.DB_NAME & ";Persist Security Info=False;"
    By the way, what's the point of setting Persist Security Info to False when there's no security to persist and you're presumably not persisting the connection string anyway?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: How to get the Application Path from DAL layer?

    Wow, that "|DataDirectory|" thingy is pretty handy, works perfectly.

    About the "Persist Security..." part of the ConnectionString, i just copy-pasted it from ConnectionString.com to my code, I see you're absolutely right, i don't even need that.

    Thank you very much JM!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to get the Application Path from DAL layer?

    FYI, |DataDirectory| resolves to the App_Data directory for ASP.NET apps and for ClickOnce apps it automatically resolves to the special folder managed by ClickOnce for data files. For other apps, it resolves to the program folder by default but you can change that in code if you like. That allows you to do things like use the common application data folder on every machine while still just using |DataDirectory| in a connection string stored in the config file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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