1

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.

enter image description here

user3448806
  • 897
  • 10
  • 22
  • Do you want to use your own defined keys, or those from analytics.xml? – Sergio Rosas Oct 25 '14 at 09:53
  • @KosmicGoat google analytic and analytics.xml here's just example :) I want to use my own keys and own setting file – user3448806 Oct 25 '14 at 09:59
  • so you want to make a library (.dll) so that when you add it to your references, some other files are copied into your solution? – mihai Oct 25 '14 at 11:23
  • @Mihai yes, in each of my other "Main project", I just add the dll to references and a "setting.xml" file to my "main project", and the dll will read data which I modified in the setting.xml – user3448806 Oct 25 '14 at 13:41
  • 1
    see my answer to your question below. see also [Can NuGet add a .cs file to the destination project](http://stackoverflow.com/questions/10785596/can-nuget-add-a-cs-file-to-the-destination-project) – mihai Oct 25 '14 at 13:56

3 Answers3

1

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;
}
danyloid
  • 1,677
  • 3
  • 21
  • 47
  • Actually I know how to read an xml file, what I want to ask is how can I read a file in my Main project from dll project ? ( in this exam, how the "google analytic" rerefence read the "analytic.xml" in my project ?) – user3448806 Oct 25 '14 at 10:49
  • @user3448806 we are probably misunderstanding each other. this is exactly the code (from the "google analytics" binaries) that reads the "analytics.xml" file. – danyloid Oct 25 '14 at 11:46
  • @user3448806 so e.g. if you will instantiate xml reader in a Windows Phone Class Library project it will read the "analytics.xml" file from your main project - that is how the `XmlReader` WP 8.0 implementation works (I'm not quite sure on the WP 8.1, though I suppose it works there as well). – danyloid Oct 25 '14 at 11:48
1

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.

mihai
  • 4,592
  • 3
  • 29
  • 42
  • perfect , this's what I need. I've also found a full tutorial [here](http://docs.nuget.org/docs/creating-packages/hosting-your-own-nuget-feeds) .Thanks :D – user3448806 Oct 25 '14 at 14:18
0

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

Community
  • 1
  • 1
Sergio Rosas
  • 545
  • 12
  • 27