一些常用公共方法

  /// <summary>
  /// 根据文件后缀名获取文件的mime类型
  /// </summary>
  /// <param name="ps_FileExtension">文件后缀名,如:.jpg,.gif</param>
  /// <returns>返回该文件的mime类型,如 image/gif</returns>
  public static string getMimeType(string ps_FileExtension)
  {
   string sResult = "";
   switch(ps_FileExtension.ToLower().Trim())
   {
    case ".gif":
     sResult = "image/gif";
     break;
    case ".jpg":
    case ".jpeg":
     sResult = "image/jpeg";
     break;
    case ".bmp":
     sResult = "image/bmp";
     break;
    case ".gz":
     sResult = "application/x-gzip";
     break;
    case ".htm":
     sResult = "text/html";
     break;
    case ".html":
     sResult = "text/html";
     break;
    case ".tar":
     sResult = "application/x-tar";
     break;
    case ".zip":
     sResult = "application/zip";
     break;
    case ".rar":
     sResult = "image/rar";
     break;
    case ".doc":
     sResult = "application/msword";
     break;
    case ".xls":
     sResult = "application/vnd.ms-excel";
     break;
    case ".ppt":
     sResult = "application/vnd.ms-powerpoint";
     break;
    default:
     sResult = "application/octet-stream";
     break;
   }

   return(sResult);
  }


  /// <summary>
  /// 根据数据表的列名取的列的序号
  /// </summary>
  /// <param name="colName">列名</param>
  /// <param name="myDataTable">数据表</param>
  /// <returns>列号,没有找到返回-1</returns>
  public static int getTableColIndexByName(string colName,DataTable myDataTable)
  {
   int iIndex = -1;
   for(int i=0;i<myDataTable.Columns.Count;i++)
   {
    if(myDataTable.Columns[i].ColumnName == colName)
    {
     iIndex = i;
     break;
    }
   }
   return iIndex;
  }

  /// <summary>
  /// 转换成全日期如2005-05-05
  /// </summary>
  /// <param name="ps_Date"></param>
  /// <returns></returns>
  public static string FromatFullDate(string ps_Date)
  {
   DateTime now = Convert.ToDateTime(ps_Date);
   string strYear = now.Year.ToString ();
   string strMonth = now.Month.ToString();
   string strDay = now.Day.ToString();
   if (strMonth.Length ==1)
   {
    strMonth="0"+strMonth;
   }
   if (strDay.Length ==1)
   {
    strDay="0"+strDay;
   }
   string strDate=strYear+"-"+strMonth+"-"+strDay;
   return strDate;
  }
  /// <summary>
  /// 转换成时间为小时分钟制,如 05:05
  /// </summary>
  /// <param name="ps_Time"></param>
  /// <returns></returns>
  public static string FromatFullTime(string ps_Time)
  {
   DateTime now = Convert.ToDateTime(ps_Time);
   string strHour = now.Hour.ToString();
   string strMinute = now.Minute.ToString();
   if (strHour.Length ==1)
   {
    strHour="0"+strHour;
   }
   if (strMinute.Length ==1)
   {
    strMinute="0"+strMinute;
   }
   string strTime=strHour+":"+strMinute;
   return strTime;
  }
  /// <summary>
  /// 转换成全日期 时:分格式,如2005-05-05 15:20
  /// </summary>
  /// <param name="ps_Date">日期</param>
  /// <param name="ps_Time">小时分钟</param>
  /// <returns></returns>
  public static string FromatFullDateTime(string ps_Date,string ps_Time)
  {
   string strDate=FromatFullDate(ps_Date);
   string strTime=FromatFullTime(ps_Time);
   return strDate+" "+strTime;
  }


  /// <summary>
  /// 判断字符是否为数字
  /// </summary>
  /// <param name="itemValue">输入的值</param>
  /// <returns>true or false</returns>
  public static bool IsNumeric(string itemValue)
  {
   return (IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", itemValue));
   //^(/+|-)?(0|[1-9]/d*)(/./d*[1-9])?$/
  }

  private static bool IsRegEx(string regExValue, string itemValue)
  {

   try
   {
    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
    if (regex.IsMatch(itemValue))
     return true;
    else
     return false;
   }
   catch (Exception )
   {
    return false;
   }
  }

  /// <summary>
  /// 编码转换
  /// </summary>
  /// <param name="strIn"></param>
  /// <param name="encoding"></param>
  /// <returns></returns>
  public static string StrConv(string strIn, string encoding)
  {
   return System.Web.HttpUtility.UrlEncode(strIn,System.Text.Encoding.GetEncoding(encoding));
  }

  /// <summary>
  /// 从SQL语句中获取表名
  /// </summary>
  /// <param name="strSql">sql语句</param>
  /// <returns></returns>
  public static string GetTableNameFromSQL(string strSql)
  {
   if (strSql=="") return "";
   string mySql;
   string strTmp;
   int i;
   mySql=strSql.ToUpper();
   i=mySql.IndexOf(" FROM ");
   strTmp=mySql.Substring(i+6);
   i=strTmp.IndexOf(" WHERE ");
   if (i>=0)
    strTmp=strTmp.Substring(0,i);
   else
   {
    i=strTmp.IndexOf(" GROUP ");
    if (i>=0) strTmp=strTmp.Substring(0,i);
    i=strTmp.IndexOf(" ORDER ");
    if (i>=0) strTmp=strTmp.Substring(0,i);
   }
   strTmp=strTmp.Trim();
   return strTmp;
  }

  /// <summary>
  /// 判断值是否在列表框中,如果在的话,就选择,否则不处理
  /// </summary>
  /// <param name="list">列表控件</param>
  /// <param name="text">值</param>
  public static void SelectListControl(ListControl list, string text)
  {
   if(text == null )
    return;
   ListItem item = list.Items.FindByText(text);
   if (item != null)
   {
    list.ClearSelection();
    item.Selected = true;
   }
   else
   {
    ListItem it = list.Items.FindByValue(text);
    if(it != null)
    {
     list.ClearSelection();
     it.Selected = true;
    }
   }
  }

  /// <summary>
  /// 取得字串的字节长度
  /// </summary>
  /// <param name="p_str"></param>
  /// <returns></returns>
  static public int GetStringByteLen(string p_str)
  {
   return(Encoding.GetEncoding("gb2312").GetByteCount(p_str));
  }

  /// <summary>
  /// 取得字串的字节长度
  /// </summary>
  /// <param name="p_source"></param>
  /// <param name="p_length"></param>
  /// <returns></returns>
  static public string CutString(string p_source ,int p_length)
  {
   return(CutString( p_source , p_length,  ".."));
  }
  
  /// <summary>
  /// 取得被截断的字串,由参数指定长度,如果超出长度就截断,并在尾部添加字串
  /// </summary>
  /// <param name="p_source">源字串</param>
  /// <param name="p_length">限定的长度</param>
  /// <param name="p_tail">尾部的字串</param>
  /// <returns></returns>
  static public string CutString(string p_source ,int p_length, string p_tail)
  {
   if(GetStringByteLen(p_source) <= p_length)
    return(p_source);
   string cutedStr;
   int cutedByLen;
   int resultLen = p_length;
   int iTailLen = GetStringByteLen(p_tail);
   
   while(true)
   {
    cutedStr = p_source.Substring(0, Convert.ToInt32(resultLen/2)); 
    cutedByLen = GetStringByteLen(cutedStr);
    if(cutedByLen + iTailLen > p_length)
     //throw new Exception("Error cutedByLen=" +cutedByLen.ToString() + "|cutedByLen=" + cutedByLen + "|iTailLen=" + iTailLen);
     CutString(cutedStr,p_length,"...");
    
    if(cutedByLen + iTailLen == p_length || cutedByLen + iTailLen == p_length-1)
    {
     return(cutedStr + p_tail);
    }
    else
    {
     resultLen += (p_length - cutedByLen -iTailLen);
    }
   }
  }

  /// <summary>
  /// 对输出在网页上的文本串进行格式化,如果该文本串为空则返回 空格
  /// 否则对该文本串执行HtmlEncode格式化,及替换换行符为 换行 操作
  /// </summary>
  /// <param name="inValue">要转换的值</param>
  /// <returns>返回文本串</returns>
  public static string DispalyEncode(string inValue)
  {
   string outValue;
   if(inValue == null)
    return("&nbsp;");
   if(inValue == "")
    return("&nbsp;");
   outValue = HttpUtility.HtmlEncode(inValue);
   //outValue = outValue.Replace("/r/n", "<br>");
   return(outValue);
  }

  /// <summary>
  /// 对输出在网页上的对象串进行格式化,如果为空则返回 一个空格
  /// </summary>
  /// <param name="p_value">要转换的对象值</param>
  /// <returns>返回文本串</returns>
  public static string DisplayEncode(object p_value)
  {
   if(p_value is DBNull)
    return("&nbsp;");
   else
    return( DisplayEncode((string)p_value) );
  }

  /// <summary>
  /// 根据原来的排序名、现有的排序名、原来的排序方式(asc或desc)判断现有的排序方式
  /// </summary>
  /// <param name="oldName">原有排序名</param>
  /// <param name="newName">现有排序名</param>
  /// <param name="oldValue">原排序方式</param>
  /// <returns>返回排序方式如desc,asc</returns>
  public static string GetSortValue(string oldName,string newName,string oldValue)
  {
   if(oldName == newName)
   {
    if(oldValue == "asc")
     return("desc");
    else
     return("asc");
   }
   else
   {
    return("asc"); 
   }
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值