这里需要注意的就是,如果是在Global.asax中添加计时器,在计时器的处理程序中就不能用Server.MapPath获得网站根目录了。

 

        public static string BasePath
        {
            
get
            {
                System.Diagnostics.Process p 
= System.Diagnostics.Process.GetCurrentProcess();
                
//WebDev.WebServer      visual studio web server
                
//xxx.vhost             Winform
                
//w3wp                  IIS7
                
//aspnet_wp             IIS6
                string processName = p.ProcessName.ToLower();
                
if (processName == "aspnet_wp" || processName == "w3wp" || processName == "webdev.webserver")
                {
                    
if (System.Web.HttpContext.Current != null)
                        
return System.Web.HttpContext.Current.Server.MapPath("~/").TrimEnd(new char[] { '\\' });
                    
else //当控件在定时器的触发程序中使用时就为空
                    {
                        
return System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' });
                    }
                }
                
else
                {
                    
return System.Windows.Forms.Application.StartupPath;
                }
            }
        }

相关文章:

  • 2021-08-04
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2021-12-23
  • 2021-06-30
猜你喜欢
  • 2021-12-21
  • 2022-02-18
  • 2022-12-23
  • 2021-10-21
  • 2021-11-10
相关资源
相似解决方案