简体   繁体   English

从可执行文件调用的 C# 程序集访问 App.Config 文件

[英]Access App.Config File from a C# Assembly called by an executable

Is it possible to access an App.Config File from a C# Class Library that is called from an exe?是否可以从 exe 调用的 C# Class 库访问 App.Config 文件?

We are running into error's and getting null when we try access the config file?当我们尝试访问配置文件时,我们遇到了错误并得到 null? I presume its to do with the originator of the call on the library, is there a work around to this other than hard coding to the file system in the Class Library?我认为它与库调用的发起者有关,除了对 Class 库中的文件系统进行硬编码之外,还有其他解决方法吗?

       try
        {
            string cleanupScripts = string.Empty;
            cleanupScripts = ConfigurationManager.AppSettings["CleanupScripts"];

            if (cleanupScripts == null)
            {
                throw new System.ApplicationException("Null was returned");
            }
        }
        catch (Exception ex)
        {
            throw (ex); 

        }

Cheers干杯

When reading app config values from a class library function, it is the app.config of the exe that is read.从 class 库 function 读取应用程序配置值时,读取的是 exe 的 app.config。 The general idea is that each client using a library should supply the relevant configuration itself.一般的想法是每个使用库的客户端都应该自己提供相关的配置。 So if you have an app.config in your class library project, that one will never be read.因此,如果您的 class 库项目中有一个 app.config,则永远不会读取该配置。

Not sure if you can do this, but I am pretty sure that C# Class Libraries allow you to use Resources.不确定您是否可以这样做,但我很确定 C# Class 库允许您使用资源。 I think its probably best for you to store the App.Config as a Resource and then access you App.config from the Resource.我认为最好将 App.Config 存储为资源,然后从资源访问 App.config。

But I guess the question is, why are you storing Configuration items inside a DLL?但我想问题是,为什么要将配置项存储在 DLL 中? why don't you create constructors on your library and have your configuration in the UI that is calling your C# Class library.为什么不在你的库上创建构造函数并在调用你的 C# Class 库的 UI 中进行配置。

when deployed app.config files are copied as ProjectName.exe.config.当部署的 app.config 文件被复制为 ProjectName.exe.config。 You should be reading this file from the class library.您应该从 class 库中读取此文件。

Hope that I understood the question correctly希望我正确理解了这个问题

Thanks Goku谢谢悟空

It's easy:这很简单:

First you go in the properties of your C# project首先你在 C# 项目的属性中的 go

在此处输入图像描述

Then to the paraméters tab然后到参数选项卡

在此处输入图像描述

Then you click to create parameters, you choose a name, a type, value, scope然后点击创建参数,选择名称,类型,值,scope

在此处输入图像描述

It creates an XML file name YourEXEFile.config in the bin folder.它会在bin文件夹中创建一个名为YourEXEFile.config的 XML 文件。 Then you can use it in your code.然后你可以在你的代码中使用它。 Or you can modify it outside your code.或者你可以在你的代码之外修改它。

static void Main(string[] args)
{
  string myP = Properties.Settings.Default.myParam1;

  Console.WriteLine(myP);
}

You can modify it in your project:你可以在你的项目中修改它:

在此处输入图像描述

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

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