如何在类库中判断当前的程序类型是Windows的和是Web的?
我现在想把业务对象层的代码放在一个类库项目中。由于有些操作对于Windows程序和Web程序是不同的,例如对于单件类的实体,如果是Windows程序可以存放在一个静态对象中(private static _singletonInstance = null);而如果是Web程序,要放在Session中(HttpContext.Current.Session["StudentSingletonInstance"] = aObject).
所以我想在类库中判断当前使用此类库的程序是Web的还是Windows的。如何实现?
我想到了一个比较笨的方法,在此抛砖引玉:
可以判断当前使用的配置文件名,如果是“Web.config”,就说明是Web程序,函数为:
private static string configFileName
{
get
{
// the configFileFullName will like "E:\VS2005\WebSites\MyRDLCDemo\web.config"
string configFileFullName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
// the configFileFullNameArray will like {'E:', 'VS2005', 'WebSites', 'MyRDLCDemo', 'web.config'}
string[] configFileFullNameArray = configFileFullName.Split(new Char[] { '\\' });
if (configFileFullNameArray.Length > 0)
{
// it returns like "web.config"
return configFileFullNameArray[configFileFullNameArray.Length - 1];
}
return "";
}
}