如何正确引用此配置文件,以便我不对路径进行硬编码?
我试图引用此配置文件,该文件与包含此代码的类位于同一文件夹中。我想对它进行某种类型的相对引用,这样我就可以从其他地方使用它。当我尝试仅使用文件名而不使用路径时,应用程序找不到该文件。我调试了它似乎在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将配置文件与 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
使用
Application.StartupPath
获取应用程序启动的路径,然后只需与文件名组合即可获取完整路径:这仅适用于 winforms。
另一种选择是使用
Environment.CurrentDirectory
- 默认情况下它将被设置为进程启动目录。请注意,此属性是可变的:Use
Application.StartupPath
to get the path the application started in then simply combine with the filename to get the full path: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:在.NET中对我有用的另一个选择是使用
Server.MapPath
。这将返回 Web 应用程序中给定相对虚拟路径的完整路径。然后可以使用该物理文件路径来创建上述文件映射。
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.This physical file path can then be used to create the file map as above.