简体   繁体   English

从dll读取Main Project中的文件

[英]Read a file in Main Project from dll

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 ?? 但是现在,当我想创建一个“ ad-control”用户控件时,我需要为每个项目使用不同的广告代码。因此,如何向我的Main项目中添加配置文件并从dll中读取该文件的密钥?

for example , I used google analytics and saw that when I install this reference to my project, they added an xml file, too. 例如 ,我使用了Google Analytics(分析),并看到当我将此引用安装到我的项目中时,他们也添加了一个xml文件。

在此处输入图片说明

I suppose you may use the XmlReader . 我想您可以使用XmlReader At least this approach is used in the GoogleAnalyticsSDK package that you seem to use. 您似乎在使用的GoogleAnalyticsSDK软件包中至少使用了这种方法。

You may check out the source code of the EasyTrackerConfig class using this link as this library source code is open. 您可以使用此链接签出EasyTrackerConfig类的源代码,因为此库源代码已打开。

Below is the code sample showing how the XmlReader could be instantiated. 下面的代码示例显示了如何实例化XmlReader

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. 然后,您可以检查EasyTrackerConfig类的LoadLoadConfigXml方法,以了解如何读取属性。 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;
}

You can create a NuGet package and specify the files you wish to be included in the final nupkg file. 您可以创建一个NuGet包并指定要包含在最终nupkg文件中的文件。

Go the your .csproj folder and run nuget spec . 转到您的.csproj文件夹并运行nuget spec Then modify the resulting projectName.nuspec file to include a files section. 然后,修改生成的projectName.nuspec文件以包含一个files部分。 Here, for example, I've indicated that I want all my txt files from the specified location copied in the target location: 例如,在这里,我表明我希望将来自指定位置的所有txt文件复制到目标位置:

  <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. target位置是相对于packages/nameOfYourDllpackages是包含您通过NuGet下载的所有dlls的文件夹。

The next step is to pack your NuGet package with nuget pack projectName.csproj . 下一步是使用nuget pack projectName.csproj打包您的NuGet包。 This will result in a projectName.nupkg file. 这将产生一个projectName.nupkg文件。 This is what you use to deploy your dll in another project through the NuGet Manager. 这就是通过NuGet Manager在另一个项目中部署dll的方式。

You can now either upload your dll to NuGet or copy it into a location on your drive. 现在,您可以将dll上传到NuGet或将其复制到驱动器上的某个位置。 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. 使用第二个选项,然后可以进入Visual Studio的“工具”->“库软件包管理器”->“软件包管理器设置”,然后从左侧菜单中选择“软件包源”。 You can then add your location where you've saved you nupkg file. 然后,您可以添加保存nupkg文件的位置。

You can now go and right-click on References in your project and go to Manage NuGet Packages. 现在,您可以在项目中的“引用”上单击鼠标右键,然后转到“管理NuGet包”。 But instead of searching on nuget.org, you can select your local NuGet 'repository' from the menu on the left. 但是,您可以从左侧菜单中选择本地的NuGet“存储库”,而不是在nuget.org上进行搜索。 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. 安装选定的本地软件包后,来自src的文件将被复制到target位置,并且您的dll将有权访问它们。 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. 我们很快就给出了这个答案,但是如果您觉得有用的话,我可以澄清一下。

You can use a config file and read the settings with the ConfigurationManager Class 您可以使用配置文件并使用ConfigurationManager类读取设置

So if you have a library project called LibraryProject, you can access a settings called myKey with 因此,如果您有一个名为LibraryProject的图书馆项目,则可以通过以下方式访问名为myKey的设置:

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). 此设置需要在主项目的配置文件中声明,如果是Web项目,则在web.config声明;对于任何其他类型,则需要在app.config中声明(如果我没记错的话)。 In these files, you can declare your own settings in the <appSettings> section, just like: 在这些文件中,您可以在<appSettings>部分中声明自己的设置,如下所示:

<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 如果确实需要仅将设置存储在库项目的配置文件中,请查看此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM