3

Is it possible to access the My.Settings of an other DLL referenced in the current Project? I have a Database project in which Settings the ConnectionString is stored. I need access to this Setting in an other Project(for Log-File).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939

2 Answers2

2

I have helped myself by a class in the Database-Project which has a function getAppSetting:

Public NotInheritable Class Helper
   Private Sub New()
   End Sub

   Public Shared Function getAppSetting(ByVal key As String) As String
       Dim returnValue As Object = My.Settings(key)
       If returnValue Is Nothing Then
           Return String.Empty
       Else
           Return returnValue.ToString
       End If
   End Function
End Class

I can call this function from my other project to get f.e. the ConnectionString.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    Better is to past the setting to the other assembly. This removes the dependency between the two assemblies, and allow you to change the database project's implementation, if needed. – AMissico Jul 03 '10 at 06:22
1

Have you tried looking at something like the System.Configuration.ConfigurationManager, I know it works for web apps, not sure about database projects.

Then you could get your connection string with a command like:

ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

Iain Hoult
  • 3,889
  • 5
  • 25
  • 39
  • My database-Project is a Class Library. The point is that i didnt know how to access its AppSettings from a different ddl. My workaround with a public function that returns it works. I think its not possible to access the AppSettings(My.Settings in .Net 2.0) from another dll(perhaps because of security concerns). – Tim Schmelter Jul 01 '10 at 16:11
  • I have tried follwing without success: http://www.knowdotnet.com/articles/accessappconfigfromdll.html (section is nothing so i got a NullReferenceException) – Tim Schmelter Jul 01 '10 at 16:22
  • Hi Tim, i think you should be able to expose your setting from the DLL with the settings in a similar way to your answer, but have you considered maybe just having your settings against your EXE instead? This is a good reason why; http://stackoverflow.com/questions/594298/c-dll-config-file/1009227#1009227 – Iain Hoult Jul 02 '10 at 15:18