简体   繁体   English

获取当前应用程序配置文件的文件路径

[英]Get the file path of current application's config file

The reason I asked this question is that I wanted to create a helper class for Remoting instantiation, and wanted to pass the appropriate app.exe.config (or web.config) file path to the RemotingConfiguration.Configure method, depending on the caller. 我问这个问题的原因是我想为Remoting实例化创建一个帮助类,并希望将相应的app.exe.config(或web.config)文件路径传递给RemotingConfiguration.Configure方法,具体取决于调用者。

Is there a way I could get the name of the config file for both Win and Web apps without checking if the application is Web or WinForms? 有没有办法我可以获得Win和Web应用程序配置文件的名称,而无需检查应用程序是Web还是WinForms?

You can use the ConfigurationFile property on the SetupInformation for AppDomain.CurrentDomain . 您可以使用ConfigurationFile财产SetupInformationAppDomain.CurrentDomain

This will get either the web.config or and app.config (yourprogram.exe.config) location. 这将获得web.config或app.config(yourprogram.exe.config)位置。

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

I've used 我用过

string folder = System.Web.HttpContext.Current != null ?
    System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data") :
    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

without problems, but maybe there is some corner cases I don't know about... 没有问题,但也许有一些我不知道的角落案例......

To find the location of executing application 查找执行应用程序的位置

System.Reflection.Assembly.GetExecutingAssembly().Location;

Don't know about web case. 关于网络案例不知道。

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

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