如何加载基于用户选择的配置文件从“未知”位置 [英] How to load a config file based on user selection from "unknown" location

查看:119
本文介绍了如何加载基于用户选择的配置文件从“未知”位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索和搜索,找不到我的具体问题的解决方案。我正在更新一个控制台应用程序,现在将用于多个客户端。我们已经决定,至少现在,将信息存储在数据库中,将客户端信息存储在配置文件中。每个客户端都有自己的配置文件。我需要知道如何从未知位置加载/任何配置文件。所有的例子,我发现希望我把文件的路径。在使用我的计算机时,我会知道路径,但一旦它被推送到其他服务器,文件的路径将改变。

I have searched and searched and can't find a solution to my specific problem. I am updating a console app that will now be used for more than one client. We have decided, over storing the info in a db at least for now, to store the clients info in config files. Each client will have their own configuration file. I need to know how to load a/any config file from an "unknown" location. All of the examples that I found want me to put in the path of the file. While using my computer, I will know the path, but once it gets pushed to other servers, the paths to the file will change.

在这些条件下,如何在不知道文件路径的情况下为任何客户端加载配置文件?

Working under these conditions, how can I load a config file for any client without knowing the path to the file?

EDIT :控制台应用程序仅在一个服务器上运行,但用于访问不同的客户端网站并抓取其网站。这就是为什么每个客户端都有自己的配置文件。它包含获取和使用其网站所需的信息。我们为应用程序设置了一个任务,以便在计时器上运行每个客户端。

EDIT: The console app is only ran on one server, but it is used to go to different clients websites and crawl their site. This is why each client has their own config file. It contains the information needed to get and use their site. We have a task set up for the app to run each client on a timer.

推荐答案

记住它是在另一个文件中使用,比如用一个include语句。所以,每个文件在另一个文件中使用,直到我们到达一个包装文件,这只是目的是定义配置文件的位置。应用程序总是通过这样的包装文件调用。这个包装文件将做任何必要的确定配置文件的位置,并使其可用于包含的文件。如果它知道用户,它可以在表中查找。这里的关键点是,当您将代码从一个环境移动到另一个环境时,包装文件不会移动。我认为这是一个有用的功能,因为我们不想每次我们将它从一个环境移动到另一个环境编辑代码。这种方法的另一个优点是它适用于所有环境,即使是非常有限的环境。例如,假设您只向程序员提供一个文件夹作为沙盒来实验代码。此程序员无法访问/ bin或/ etc目录。相比之下,在这种情况下,建议的方法将正常工作,因为程序员可以设置配置文件,无论他想要在其本地包装文件。此问题在这里讨论如何组织代码,以便我们可以移动和更新它,而无需编辑配置文件的位置?

What about writing every piece of code with in mind that it is to be used in another file, say with an include statement. So, every file is used in another one until we reach a wrapper file, which only purpose is to define the location of the config file. The application would always be called through such a wrapper file. This wrapper file will do whatever necessary to determine the location of the config file and make it available to the included files. If it knows the user, it can look up in a table. The key point here is that, the wrapper files would not move when you move the code from one environment to another. I think that this is a useful feature, because we do not want edit the code each time that we move it from one environment to another. Another advantage of this approach is that it applies to all environments, even very limited environments. For example, let say you provide only a folder to a programmer as a sandbox to experiment with the code. This programmer does not have access to the /bin or the /etc directory. In contrast, the proposed approach will work fine in this case, because the programmer can set the config files wherever he wants in its local wrapper files. This issue is discussed here How to organize code so that we can move and update it without having to edit the location of the configuration file?

这篇关于如何加载基于用户选择的配置文件从“未知”位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆