在开发过程中,经常需要获取程序执行的路径 ,以便得到配置文件的位置.而有些程序是windows应用程序,有些是Website页面,还有的是Window服务程序,获取的方式各不相同.下面一段代码,可以适用不同应用环境:
public string GetConfigPath(){
string strFile;
if (System.Web.HttpContext.Current != null)
{
strFile = System.Web.HttpRuntime.AppDomainAppPath+ "//Config//config.xml";
}
else
{
string fileName = Process.GetCurrentProcess().MainModule.FileName;
strFile = fileName.Substring(0, fileName.LastIndexOf('//') + 1) + "Config//config.xml";
}
return strFile;
}