如何正确引用此配置文件,以便我不对路径进行硬编码?

发布于 2024-10-01 03:15:06 字数 711 浏览 1 评论 0原文

我试图引用此配置文件,该文件与包含此代码的类位于同一文件夹中。我想对它进行某种类型的相对引用,这样我就可以从其他地方使用它。当我尝试仅使用文件名而不使用路径时,应用程序找不到该文件。我调试了它似乎在 IIS 文件夹中查找的文件夹,这很有意义,因为我在 IIS 托管的 wcf 服务中使用它。无论如何,我如何正确引用这个配置文件而不对路径进行硬编码?所以它看起来在项目位置。感谢您的任何帮助。周末愉快!

 public void Init()
    {
        var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = @"C:\workspace\new\UnityDemo-v1.0.0.1\src\Core\unity.config" };

        Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
        _container = new UnityContainer().LoadConfiguration(unitySection);
    }

干杯,
〜ck

Im trying to reference this config file that is in the same folder as the class that contains this code. I'd like to do some type of relative reference to it, so I can use it from other places. When I try using just the file name without the path, the application doesn't find the file. I debugged and the folder it seems to be looking in IIS folder which makes sense as Im using it in an IIS hosted wcf service. Anyways, how I can properly reference this config file without hard coding the path? So it looks in the project location. Thanks for any help. Have a great weekend!

 public void Init()
    {
        var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = @"C:\workspace\new\UnityDemo-v1.0.0.1\src\Core\unity.config" };

        Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
        _container = new UnityContainer().LoadConfiguration(unitySection);
    }

Cheers,
~ck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏の忆 2024-10-08 03:15:06

将配置文件与 IIS 中托管的服务一起使用是很棘手的,因为应用程序目录是 IIS 运行的目录,并且将受到严格保护,防止在其中放置任何文件。可能还有其他方法,但对我来说,可以将文件命名为 web.config 并将其复制到 .svc 文件所在的目录,然后您可以直接读取设置,而无需引用配置文件。我不知道有什么方法可以从程序本身内部进行复制。不过安装程序将能够做到这一点。
请参阅:此问题

Using config files with services hosted in IIS is tricky, because the application directory is the one IIS runs in and that will be heavily protected against placing any files there. There may be other ways but for me it works to name the file web.config and copy it to the directory the .svc file resides in and then you can read the settings directly without having to reference the config file. I do not know of any way to do this copying from within the program itself. The installer will be able to do it though.
See: this question

月牙弯弯 2024-10-08 03:15:06

使用 Application.StartupPath 获取应用程序启动的路径,然后只需与文件名组合即可获取完整路径:

var filePath = Path.Combine(Application.StartupPath, "unity.config");
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = filePath };

这仅适用于 winforms。


另一种选择是使用 Environment.CurrentDirectory - 默认情况下它将被设置为进程启动目录。请注意,此属性是可变的:

var filePath = Path.Combine(Environment.CurrentDirectory, "unity.config");
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = filePath };

Use Application.StartupPath to get the path the application started in then simply combine with the filename to get the full path:

var filePath = Path.Combine(Application.StartupPath, "unity.config");
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = filePath };

This will work only in winforms.


Another option is to use Environment.CurrentDirectory - by default it will be set to the process startup directory. Note that this property is mutable:

var filePath = Path.Combine(Environment.CurrentDirectory, "unity.config");
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
梦开始←不甜 2024-10-08 03:15:06

在.NET中对我有用的另一个选择是使用 Server.MapPath。这将返回 Web 应用程序中给定相对虚拟路径的完整路径。

var filePath = Server.MapPath("/unity.config")

然后可以使用该物理文件路径来创建上述文件映射。

Another option that worked for me in .NET is using Server.MapPath. This returns the full path of the given relative virtual path in the web application.

var filePath = Server.MapPath("/unity.config")

This physical file path can then be used to create the file map as above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文