Reputation: 907
I created a project which contains many custom control to re-use in another projects. But now when I want to create a "ad-control" usercontrol, I need different ad-code for each project.So how can I add a config file to my Main project and read keys of this file from my dll ??
for example, I used google analytics and saw that when I install this reference to my project, they added an xml file, too.
Upvotes: 1
Views: 294
Reputation: 545
You can use a config file and read the settings with the ConfigurationManager Class
So if you have a library project called LibraryProject, you can access a settings called myKey
with
string value = ConfigurationManager.AppSettings["myKey"];
This setting needs to be declared in a configuration file in the main project, a web.config
if a web project or an app.config
for any other type (if I'm not mistaken). In these files, you can declare
your own settings in the <appSettings>
section, just like:
<appSettings>
<add key="myKey" value="myValue"/>
</appSettings>
If you really need to store the settings only in a configuration file in the library project, take a look to this question
Upvotes: 0
Reputation: 4722
You can create a NuGet package and specify the files you wish to be included in the final nupkg
file.
Go the your .csproj
folder and run nuget spec
. Then modify the resulting projectName.nuspec
file to include a files
section. Here, for example, I've indicated that I want all my txt files from the specified location copied in the target location:
<files>
<file src="headers\*.txt" target="content\headers" />
</files>
The target
location is relative to the packages/nameOfYourDll
and packages
is the folder containing all the dlls
you've downloaded through NuGet.
The next step is to pack your NuGet package with nuget pack projectName.csproj
. This will result in a projectName.nupkg
file. This is what you use to deploy your dll in another project through the NuGet Manager.
You can now either upload your dll to NuGet or copy it into a location on your drive. With this second option, you can then go in Visual Studio in Tools --> Library Package Manager --> Package Manager Settings and select Package Sources from the left menu. You can then add your location where you've saved you nupkg
file.
You can now go and right-click on References in your project and go to Manage NuGet Packages. But instead of searching on nuget.org, you can select your local NuGet 'repository' from the menu on the left. After you install the selected local package, the files from src
will be copied to the target
location and your dll will have access to them. You'll just have to find meaningful paths now.
This answer was given in a hurry, but I can add clarifications if you find it useful.
Upvotes: 1
Reputation: 1677
I suppose you may use the XmlReader
. At least this approach is used in the GoogleAnalyticsSDK
package that you seem to use.
You may check out the source code of the EasyTrackerConfig
class using this link as this library source code is open.
Below is the code sample showing how the XmlReader
could be instantiated.
var xmlReader = XmlReader.Create("analytics.xml");
Then you may check out the Load
and LoadConfigXml
methods of EasyTrackerConfig
class to see how the attributes are read.
Just in case they are provided below:
internal static EasyTrackerConfig Load(XmlReader reader)
{
// advance to first element
while (reader.NodeType != XmlNodeType.Element && !reader.EOF)
{
reader.Read();
}
if (!reader.EOF && reader.Name == "analytics")
{
return LoadConfigXml(reader);
}
return new EasyTrackerConfig();
}
private static EasyTrackerConfig LoadConfigXml(XmlReader reader)
{
var result = new EasyTrackerConfig();
reader.ReadStartElement("analytics");
do
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "trackingId":
result.TrackingId = reader.ReadElementContentAsString();
break;
case "appName":
result.AppName = reader.ReadElementContentAsString();
break;
case "appVersion":
result.AppVersion = reader.ReadElementContentAsString();
break;
case "appId":
result.AppId = reader.ReadElementContentAsString();
break;
case "appInstallerId":
result.AppInstallerId = reader.ReadElementContentAsString();
break;
case "sampleFrequency":
result.SampleFrequency = reader.ReadElementContentAsFloat();
break;
case "dispatchPeriod":
var dispatchPeriodInSeconds = reader.ReadElementContentAsInt();
result.DispatchPeriod = TimeSpan.FromSeconds(dispatchPeriodInSeconds);
break;
case "sessionTimeout":
var sessionTimeoutInSeconds = reader.ReadElementContentAsInt();
result.SessionTimeout = (sessionTimeoutInSeconds >= 0) ? TimeSpan.FromSeconds(sessionTimeoutInSeconds) : (TimeSpan?)null;
break;
case "debug":
result.Debug = reader.ReadElementContentAsBoolean();
break;
case "autoAppLifetimeTracking":
result.AutoAppLifetimeTracking = reader.ReadElementContentAsBoolean();
break;
case "autoAppLifetimeMonitoring":
result.AutoAppLifetimeMonitoring = reader.ReadElementContentAsBoolean();
break;
case "anonymizeIp":
result.AnonymizeIp = reader.ReadElementContentAsBoolean();
break;
case "reportUncaughtExceptions":
result.ReportUncaughtExceptions = reader.ReadElementContentAsBoolean();
break;
case "useSecure":
result.UseSecure = reader.ReadElementContentAsBoolean();
break;
case "autoTrackNetworkConnectivity":
result.AutoTrackNetworkConnectivity = reader.ReadElementContentAsBoolean();
break;
default:
reader.Skip();
break;
}
}
else
{
reader.ReadEndElement();
break;
}
}
while (true);
return result;
}
Upvotes: 1