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).
A:
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
2010-07-01 10:55:15
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
2010-07-03 06:22:23
+1
A:
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
2010-07-01 15:47:41
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
2010-07-01 16:11:38
I have tried follwing without success: http://www.knowdotnet.com/articles/accessappconfigfromdll.html(section is nothing so i got a NullReferenceException)
Tim Schmelter
2010-07-01 16:22:23
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
2010-07-02 15:18:55