.net一些常用静态类

using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.IO;
using System.Web;
namespace Main.components
{
 /// <summary>
 /// baseFunc 的摘要说明。
 /// </summary>
 public class baseFunc
 {
  public baseFunc()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  #region 字符串截取函数
  /// <summary>
  /// 字符串截取函数
  /// </summary>
  /// <param name="str"></param>
  /// <param name="num"></param>
  /// <returns></returns>
  public static string CutString(string str,int num)
  {
   str=RemoveHTML(str);
   if(str.Length>num)
   {
    str=str.Substring(0,num)+"..";
   }
   return str;
  }
  public static string CutStringYes(string str,int num)
  {
   
   if(str.Length>num)
   {
    str=str.Substring(0,num)+"..";
   }
   return str;
  }
  public static string CutStringNo(string str,int num)
  {
   
   if(str.Length>num)
   {
    str=str.Substring(0,num);
   }
   return str;
  }
  #endregion
  public static string RemoveHTML(string strHTML)
  {
   System.Text.RegularExpressions.Regex Regexp=new System.Text.RegularExpressions.Regex("<.+?>");
   string strReturn=Regexp.Replace(strHTML,"");
   return strReturn; 
  }
  #region 弹出窗口
  /// <summary>
  /// 弹出窗口
  /// </summary>
  /// <param name="opener">控件名称</param>
  /// <param name="PagePath">弹出页面的路径</param>
  public static void  OpenPopUp(System.Web.UI.WebControls.WebControl opener,string PagePath)
  {
   string clientScript;
   clientScript = "window.open('" + PagePath + "')";
   opener.Attributes.Add("onClick", clientScript);
  }
  /// <param name="opener">控件名称</param>
  /// <param name="PagePath">弹出页面的路径</param>
  /// <param name="WindowName">窗口名字</param>
  /// <param name="width">宽度</param>
  /// <param name="height">高度</param>
  public static void  OpenPopUp(System.Web.UI.WebControls.WebControl opener,string PagePath,string WindowName,int width,int height)
  {
   string clientScript;
   string windowAttribs;
   windowAttribs="width=" + width.ToString() + "px,"+"height="+height.ToString()+"px,"+"left='+((screen.width -" + width.ToString() + ") / 4)+',"+"top='+ (screen.height - " + height.ToString() + ") / 4+' scrollbars=yes";
   clientScript = "window.open('" + PagePath + "','" + WindowName + "','" + windowAttribs + "');return false;";
   opener.Attributes.Add("onClick", clientScript);
  }
  #endregion
  #region 将DataReader 转为 DataTable
  /// <summary>
  /// 将DataReader 转为 DataTable
  /// </summary>
  /// <param name="DataReader">DataReader</param>
  public static  DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
  {
   DataTable datatable = new DataTable();        
   DataTable schemaTable = dataReader.GetSchemaTable();    //描述 dataReader 的列元数据         
   //动态添加列
   try
   {
    foreach(DataRow myRow in schemaTable.Rows)
    {
     DataColumn myDataColumn = new DataColumn();
     myDataColumn.DataType = myRow.GetType();
     myDataColumn.ColumnName = myRow[0].ToString();
     datatable.Columns.Add(myDataColumn);
    }
    //添加数据
    while(dataReader.Read())
    {
     DataRow myDataRow = datatable.NewRow();
     for(int i=0;i<schemaTable.Rows.Count;i++)
     {
      myDataRow[i] = dataReader[i].ToString();
     }
     datatable.Rows.Add(myDataRow);
     myDataRow = null;
    }
    schemaTable = null;
    return datatable;
   }
   catch(Exception ex)
   {
    return datatable;
   }
  }
  #endregion
  #region 判断网址是h/w开头
  public static string ReturnWebadd(string str)
  {
   
   string str2;
   if(str!=null&&str.ToString()!="")
   {
    string str1=str.Substring(0,1).ToString();
    if(str1=="w"||str1=="W")
    {
     str2=" http://"+str;
    }
    else
    {
     str2=str;
    }
   }
   else
   {
    str2="";
   }
   return str2;
  }
  #endregion
  #region 图片New的显示
  /// <summary>
  /// 图片New的显示
  /// </summary>
  /// <param name="str"></param>
  /// <param name="num"></param>
  /// <returns></returns>
  public static string DispNew(DateTime StrDate)
  {
   string imgStr="";
   //DateTime DateStr=new DateTime();
   //DateTime DateStr=StrDate;
   //DateTime TodayStr=DateTime.Now;
   int CtoT=0;
   TimeSpan dt = DateTime.Now.Date.Subtract(StrDate);
   double subDay = dt.Days;
   CtoT=Convert.ToInt32(subDay);
   if(CtoT>=0 && CtoT<=7)
   {
    imgStr="<img src='../images/04.gif' border='0' width='23' height='11'>";
   }
   return imgStr;
   //return "["+StrDate.ToString()+"]"+CtoT.ToString();
  }
  #endregion
 
 #region 取得当前全部网址
  public static string getURL()
  {
   string strTemp="";
   if(HttpContext.Current.Request.ServerVariables["HTTPS"].ToString().ToLower()=="off")
   {
    strTemp="http://";
   }
   else
   {
    strTemp="https://";
   }
   strTemp = strTemp+HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
   if( Convert.ToInt32(HttpContext.Current.Request.ServerVariables["SERVER_PORT"])!= 80) 
    strTemp = strTemp+":"+HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
   strTemp = strTemp+HttpContext.Current.Request.ServerVariables["URL"];
   if (HttpContext.Current.Request.QueryString.ToString().Trim()!= "")
    strTemp = strTemp+"?"+HttpContext.Current.Request.QueryString.ToString().Trim();
   string GetUrl = strTemp;
   return GetUrl;
  }
  #endregion
  
 }
}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值