ASP.NET(c#)常用类函数

常用函数系列:
public static string Get_ClientIP() 得到客户端IP
public static string Get_CMac(string IP) 得到客户端 CMac 地址
public static string RequestF(string xPName,string xPType,int xLenDef) 安全接收数据系列
public static string Show_Cont(string xStr) 过滤显示字串
public static string Show_jsStr(string xStr) 过滤显示js

安全检测函数:
public static string CheckUrl(string xDirPage) 上页地址认证
public static string Chk_Perm0(string xPerm,string xSys,string xAct) 权限认证系列
public static string Chk_Perm1(string xPerm,string xSys,string xAct) 权限认证系列
public static string Chk_Perm2(string xPerm,string xSys,string xAct) 权限认证系列

邮件发送函数:
public void SendEmail(string xSubj,string xCont,string FmAddr,string ToAddr) 
public void SendSmtp(string xSubj,string xCont,string xFrom,string xTo)

加密解密函数:
public static string Enc_PW(string xID,string xPW,int xLen) 改装sha1+md5加密解密函数加密函数
public static string Conv_10toXX(long xNum,int xBase) 10进制 转 XX 进制
public static long Conv_XXto10(string xStr,int xBase) xx进制 转 10 进制
public static string DESDec(string pToDecrypt, string sKey) DES解密
public static string DESEnc(string pToEncrypt, string sKey) DES加密密
public static string DESPeace(string xStr, string xType) 改装DES
public static string DESSwap(string xStr, int xN) DES 改装算法

文件操作函数:
public static string fCreate(string xFile,string xContent) 建立文件
public void ImgCode (Page containsPage,string validateNum) 生成图片认证码
public static string ImgShow(string xPName,int xImgW,int xImgH,int xMaxW,int xMaxH) 按比例大小显示图片
public static DataTable fList(string xPath) 显示文件列表 显示文件夹列表
public static string fRead(string xFile) 文件读取函数
public static ArrayList fUpload(HttpPostedFile xFile,string xPath,string xOrg,int xSize,string xType) 文件上传认证函数


时间/随即字串函数:
public static string Get_AutoID(int xLen) 自动随机 ID 串
public static string Get_HHMMSS()  得到时间HHmmss格式
public static string Get_TimeID()  得到年月日时间YYYY-MM-DD HH:mm:ss格式
public static string Get_YYYYMMDD() 得到年月日YYYYMMDD格式
public static string Get_mSec() 得到毫秒
public static string Rnd_ID(string xType,int xLen)  自动随机 ID 串
public static int Rnd_NM(int xN,int xM)  自动随机 N ~ M 数字


数据库操作函数:
public static void rs_AddLog(string xconn,string xUSID,string xAct,string xSys,string xNote) 添加 Log
public static int rs_Count(string xConn,string xSQL,string xTable) 计算记录条数
public static void rs_DoSQL(string xConn,string xSQL) 执行SQL语句
public static string rs_Exist(string xConn,string xSQL,string xTable) 检查是否存在
public static void rs_List(DropDownList xDDList,string xConn,string xSql,string xDefValue,string xClear) 绑定DropDownList
public static string rs_Val(string xConn,string xSQL,string xTable,string xCol) 得到特定 字段值

 

 


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web;
using System.Web.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using PubSet;

namespace Peace
{
 public class WebCS 
 {  
  public static string RequestQ(string xPName,string xPType,int xLenDef)
  {
   string PValue = HttpContext.Current.Request.QueryString[xPName]; 
   return RequestS(PValue,xPType,xLenDef); 
  }
  public static string RequestF(string xPName,string xPType,int xLenDef)
  {
   string PValue = HttpContext.Current.Request.Form[xPName]; 
   return RequestS(PValue,xPType,xLenDef); 
  }
  public static string RequestA(string xPName,string xPType,int xLenDef)
  {
   string PValue = HttpContext.Current.Request[xPName]; 
   return RequestS(PValue,xPType,xLenDef); 
  }

  public static string RequestS(string xPName,string xPType,int xLenDef)
  {
   string PValue = xPName+"";  
   string tmpType = xPType;
   switch (tmpType)
   {
    case "N": // Number -1,0,1
     try  { string tI = (int.Parse(PValue)).ToString(); }
     catch { PValue = xLenDef.ToString();} 
     break;
    case "D": // Date 
     try  { DateTime tD = DateTime.Parse(PValue); }
     catch 
     { 
      PValue = xLenDef.ToString();
      PValue = PValue.Substring(0,4)+"-"+PValue.Substring(4,2)+"-"+PValue.Substring(6,2);
     } 
     break;
    default: // Text xLenDef = 19001231
     if(PValue.Length > xLenDef) { PValue = PValue.Substring(0,xLenDef); }
     PValue = PValue.Replace("'","''");
     break;
   }
   return PValue;
  } 

  public static string Show_Text(string xStr)
  {
   string tStr = xStr;
   tStr = tStr.Replace("<","&lt;");
   tStr = tStr.Replace(">","&gt;");
   tStr = tStr.Replace("\r","<br>");
   tStr = tStr.Replace("  ","&nbsp;&nbsp;");
   tStr = tStr.Replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;");
   return tStr;
  }

  public static string Show_Cont(string xStr)
  {
   string tStr = Show_Text(xStr);
   return tStr;
  }

  public static string Show_Form(string xStr)
  {
   string tStr = xStr;
   tStr = tStr.Replace("\'","'");
   tStr = tStr.Replace("\"",""");
   tStr = tStr.Replace("<","&lt;");
   tStr = tStr.Replace(">","&gt;");
   return tStr;
  }

  public static string Show_jsStr(string xStr)
  {
   string tStr = xStr;
   tStr = tStr.Replace("\'","\\\'");
   tStr = tStr.Replace("\"","\\\"");
   return tStr;
  }

  public static string Get_ClientIP()
  {
   string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
   if (null == result || result == String.Empty) 
   {
    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
   }
   if (null == result || result == String.Empty) 
   {
    result = HttpContext.Current.Request.UserHostAddress;
   }
   return result;
  }

  public static string Get_CMac(string IP) //para IP is the client@#s IP 
  { 
   string dirResults=""; //IP = "192.168.37.175";//"211.156.182.34";
   ProcessStartInfo psi  = new ProcessStartInfo(); 
   Process proc = new Process(); 
   psi.FileName = "nbtstat"; 
   psi.RedirectStandardInput = false; 
   psi.RedirectStandardOutput = true; 
   psi.Arguments = "-A " + IP; 
   psi.UseShellExecute = false; 
   proc = Process.Start(psi); 
   dirResults = proc.StandardOutput.ReadToEnd(); 
   proc.WaitForExit(); 
   dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t",""); 
   Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?)) __MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled); 
   Match mc=reg.Match(dirResults+"__MAC"); 
   if(mc.Success) 
   { 
    return mc.Groups["key"].Value; 
   } 
   else 
   { 
    reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled); 
    mc=reg.Match(dirResults); 
    if(mc.Success) { return "Host not found!"; } 
    else   { return "N/A"; } 
   } 
  }
 }

 public class WebID 
 { 
  public static string Get_TimeID()
  {
   string YMDHMS = System.DateTime.Now.ToString("yyyyMMddHHmmss");
   string mSec = Get_mSec();
   return YMDHMS+mSec;
  }

  public static string Get_YYYYMMDD()
  {
   return System.DateTime.Now.ToString("yyyyMMdd");
  }

  public static string Get_HHMMSS()
  {
   return System.DateTime.Now.ToString("HHmmss");
  }

  public static string Get_mSec()
  {
   string mSec = System.DateTime.Now.Millisecond.ToString();
   mSec = "00" + mSec;
   return mSec.Substring(mSec.Length-3,3);
  }

  public static string Get_AutoID(int xLen)
  {
   long tNum = DateTime.Now.Ticks;
   string tStr = tNum.ToString("X16");
   if(xLen<tStr.Length) { tStr = tStr.Substring(0,xLen); }
   else     { tStr += Rnd_ID("KEY",xLen-tStr.Length); }
   return tStr; 
  }

  public static string Rnd_ID(string xType,int xLen)
  {
   string rChar;
   int rMax,i;
   string orgNum = "0123456789";     //  10   xType = 0,A,KEY
   string orgCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //  26   .  -  _  $  |  !  #  [  ]  
   string orgKey = "ABCDEFGHJKLMNPQRSTUVWXY";      //  23   I  O  Z
   string oStr = "";
   string tStr = "";
   switch (xType)
   {
    case "0": rMax = 10; tStr = orgNum; break;
    case "A": rMax = 26; tStr = orgCap; break;
    default: rMax = 33; tStr = orgNum + orgKey; break; 
   }
   System.Random ran = new Random(System.DateTime.Now.Second + (int)System.DateTime.Now.Ticks); 
   for (i=0;i<Math.Abs(xLen);i++)
   {
    int rin = ran.Next(0,rMax-1);
    rChar = tStr.Substring(rin,1);
    oStr += rChar;
   }
   return oStr;
  }

  public static int Rnd_NM(int xN,int xM)
  {
   System.Random ran = new Random(System.DateTime.Now.Second + (int)System.DateTime.Now.Ticks); 
   return ran.Next(xN,xM);
  }
 }

 public class WebRS 
 {
  public static void rs_AddLog(string xconn,string xUSID,string xAct,string xSys,string xNote)
  {
   string Tim,IP,PNow,PRef,sql;
   Tim = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+Peace.WebID.Rnd_ID("KEY",5); 
   IP = Peace.WebCS.Get_ClientIP() ;
   PNow = HttpContext.Current.Request.Url.ToString(); ;
   PRef = HttpContext.Current.Request.ServerVariables["http_referer"]+"";
   PNow = Peace.WebCS.RequestS(PNow,"",96);
   PRef = Peace.WebCS.RequestS(PRef,"",96);
   xAct = xAct.Replace("'",""); 
   xNote = Peace.WebCS.RequestS(xNote,"",255); 
   if (PRef.IndexOf("?")>0) { PRef = PRef.Substring(0,PRef.IndexOf("?")); }
   if (PNow.IndexOf("?")>0) { PNow = PNow.Substring(0,PNow.IndexOf("?")); }
   sql  = " INSERT INTO AdmLogs (LGTime,USID,LGAct,LGPag1,LGPag2,LGSyst,LGNote,LGIP) VALUES ";
   sql += " ('"+Tim+"','"+xUSID+"','"+xAct+"','"+PNow+"','"+PRef+"','"+xSys+"','"+xNote+"','"+IP+"')";
   Peace.WebRS.rs_DoSQL(xconn,sql);
  }

  public static string rs_Val(string xConn,string xSQL,string xTable,string xCol)
  {
   string rObj;
   SqlConnection cnRec = new SqlConnection(xConn);
   SqlDataAdapter cmRec = new SqlDataAdapter(xSQL, cnRec);
   DataSet dsRec = new DataSet();
   cmRec.Fill(dsRec, xTable);
   if(dsRec.Tables[0].Rows.Count>0)
   {
    DataRow curDR;
    curDR = dsRec.Tables[0].Rows[0];
    rObj  = curDR[xCol].ToString(); 
   }
   else { rObj = ""; }
   return rObj;
  }

  public static string rs_Exist(string xConn,string xSQL,string xTable)
  {
   string rObj;
   SqlConnection cnRec = new SqlConnection(xConn);
   SqlDataAdapter cmRec = new SqlDataAdapter(xSQL, cnRec);
   DataSet dsRec = new DataSet();
   cmRec.Fill(dsRec, xTable);
   if(dsRec.Tables[0].Rows.Count>0) { rObj = "YES"; }
   else { rObj = "EOF"; }
   return rObj;
  }

  public static int rs_Count(string xConn,string xSQL,string xTable)
  {
   SqlConnection cnRec = new SqlConnection(xConn);
   SqlDataAdapter cmRec = new SqlDataAdapter(xSQL, cnRec);
   DataSet dsRec = new DataSet();
   cmRec.Fill(dsRec, xTable);
   return dsRec.Tables[0].Rows.Count;
  }

  public static void rs_DoSQL(string xConn,string xSQL)
  {
   SqlConnection conn = new SqlConnection(xConn);
   conn.Open();
   SqlCommand insCMD = new SqlCommand(xSQL,conn);
   Int32 recordsAffected = insCMD.ExecuteNonQuery();
   conn.Close();
  } 

  public static void rs_List(DropDownList xDDList,string xConn,string xSql,string xDefValue,string xClear)
  {
   string conn=PubSet.Config.cnStr; //HttpContext.Current
   SqlConnection cn = new SqlConnection( conn );
   SqlCommand cm = new SqlCommand( xSql, cn ); 
   cm.Connection.Open(); 
   SqlDataReader iReader = cm.ExecuteReader(); 
   if (xClear.Length>0)   // Clear Old Records;
     { xDDList.Items.Clear(); }
   while ( iReader.Read() ) 
   { xDDList.Items.Add( new ListItem( iReader[1].ToString(),iReader[0].ToString() ) ); } 
   if(xDDList.Items.Count==0)  // If NOT found record,Add blank value,but NOT [Null]
     { xDDList.Items.Add( new ListItem( "", "" ) ); }  
   cm.Connection.Close(); 
  }

 }

 public class WebCheck 
 {
  public static string CheckUrl(string xDirPage)
  {
   string sPrev = HttpContext.Current.Request.ServerVariables["http_referer"]+"";
   string sNow  = HttpContext.Current.Request.Url.ToString();
   sPrev = sPrev.ToLower();
   sPrev = sPrev.Replace("http://","");
   sPrev = sPrev.Replace("www.","");
   if(sPrev.IndexOf("/",0)>0)
   { 
    sPrev = sPrev.Substring(0,sPrev.IndexOf("/",0));
   }
   sNow  = sNow.ToLower();
   if( (sPrev.Length==0) || (sNow.IndexOf(sPrev,0)<0) )
   {
    if(xDirPage=="") { /*xObj.Response.Redirect(Config.WHome);*/ }
    else    { HttpContext.Current.Response.Redirect(xDirPage); }   
   }
   return sPrev ;
  }

  public static string Chk_Perm1(string xSys,string xAct,string xPath)
  {
   string AdmPerm = (string)HttpContext.Current.Session["AdmPerm"]+"";
   string flgPerm = "Error";
   if(AdmPerm.Length<3)  { flgPerm = "Error"; }
   else      { flgPerm = Chk_Perm0(AdmPerm,xSys,xAct); }
   if( flgPerm=="Error" )
   {
    if(xPath=="")   { /*xObj.Response.Redirect(Config.WHome);*/ }
    else     { HttpContext.Current.Response.Redirect(xPath); }  
   }
   return flgPerm ;
  }

  public static string Chk_Perm2(string xSys,string xAct,string xPath)
  {
   string AdmPerm = (string)HttpContext.Current.Session["AdmPerm"]+"";
   string MemPerm = (string)HttpContext.Current.Session["MemPerm"]+"";
   string flgPerm = "Error";
   if(MemPerm.Length<3)  { flgPerm = "Error"; }
   else      { flgPerm = Chk_Perm0(MemPerm,xSys,xAct); }
   if(AdmPerm.Length>3)  { flgPerm = "Pass"; }
   if( flgPerm=="Error" )
   {
    if(xPath=="")   { HttpContext.Current.Response.Redirect("/"); }
    else     { HttpContext.Current.Response.Redirect(xPath); }  
   }
   return flgPerm ;
  }

  public static string Chk_Perm0(string xPerm,string xSys,string xAct)
  {
   string flgPerm = "(N/A)";
   if(xSys=="")   { flgPerm = "Pass"; }
   else 
   {
    if(xPerm.IndexOf("("+xSys+")")>=0)
    {
     if(xAct=="") { flgPerm = "Pass"; }     
     else 
     {
      int p1 = xPerm.IndexOf("("+xSys+")");
      int p2 = xPerm.IndexOf("(/"+xSys+")");
      if( (p1>=0) && (p2>p1) )
      {
       xPerm = xPerm.Substring(p1,p2-p1);
       xAct = xAct.ToUpper();
       if(xPerm.IndexOf(""+xAct+"")>=0)
       { flgPerm = "Pass"; }
       else { flgPerm = "Error"; }
      }
      else  { flgPerm = "Error"; }
     }
    }
    else    { flgPerm = "Error"; }     
   }
   return flgPerm ;
  }

 }

 public class WebEnc 
 {
  public static string Enc_PW(string xID,string xPW,int xLen)
  {
   string eStr1 = FormsAuthentication.HashPasswordForStoringInConfigFile(xID,"sha1"); // 40
   string eStr2 = "";
   string tStr;
   for(int i=0; i<4; i++)
   {
    tStr = eStr1.Substring(10*i,10);
    eStr2 += FormsAuthentication.HashPasswordForStoringInConfigFile(xPW+tStr,"md5"); // 32
   }
   int ne1 = (128 - xLen)/2; 
   return eStr2.Substring(ne1,xLen);
  }

  public static string DESPeace(string xStr, string xType)
  {
   string oStr,sKey=PubSet.Config.DESOffset;
   if (xType=="Enc") 
   { 
    oStr = DESEnc(xStr,sKey); 
    oStr = Peace.WebEnc.DESSwap(oStr,4);
   }
   else    
   { 
    xStr = Peace.WebEnc.DESSwap(xStr,4);
    oStr = DESDec(xStr,sKey);
   }
   return oStr;
  }

  public static string DESSwap(string xStr, int xN)
  {
   int sLen; 
   string s1="",s2="",s3="";
   sLen = xStr.Length;
   if ( (xN>1) && (xN<sLen) )
   {
    s1 = xStr.Substring(0,xN);
    s2 = xStr.Substring(sLen-xN,xN);
   }
   for(int i=(sLen-xN-1);i>=xN;i--)
   {
    s3 += xStr.Substring(i,1);
   }
   return s1 + s3 + s2; // xStr.Substring(xN,sLen-(2*xN))
  }

  public static string DESEnc(string pToEncrypt, string sKey)
  {
   DESCryptoServiceProvider des = new DESCryptoServiceProvider();
   byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
   des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
   des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
   MemoryStream ms = new MemoryStream();
   CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);
   cs.Write(inputByteArray, 0, inputByteArray.Length);
   cs.FlushFinalBlock();
   StringBuilder ret = new StringBuilder();
   foreach(byte b in ms.ToArray())
   {
    ret.AppendFormat("{0:X2}", b);
   }
   ret.ToString();
   return ret.ToString();
  }

  public static string DESDec(string pToDecrypt, string sKey)
  {
   DESCryptoServiceProvider des = new DESCryptoServiceProvider();
   byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
   for(int x = 0; x < pToDecrypt.Length / 2; x++)
   {
    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
    inputByteArray[x] = (byte)i;
   }
   des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
   des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
   MemoryStream ms = new MemoryStream();
   CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
   cs.Write(inputByteArray, 0, inputByteArray.Length);
   cs.FlushFinalBlock();
   StringBuilder ret = new StringBuilder(); // 把解密后的文本变成流对象
   return System.Text.Encoding.Default.GetString(ms.ToArray());
  }

  public static string Conv_10toXX(long xNum,int xBase)
  {
   string oStr = "0123456789ABCDEFGHJKLMNPQRSTUVWXY";
   long nm = xNum; 
   long ni = nm / xBase;
   long nr = nm % xBase;
   int  nk = (int)nr;
   string ch = "";
   string ns = "";
   while (nm >= xBase)
   {
    ni = nm / xBase;
    nr = nm % xBase;
    nk = (int)nr;
    ch = oStr.Substring(nk,1); 
    ns += ch;
    nm = ni;
   }
   if(nm!=0)
   {
    nk = (int)nm;
    ch = oStr.Substring(nk,1); 
    ns += ch;
   }
   string rs = "";
   for(int i=ns.Length; i>0; i--)
   {
    rs += ns.Substring(i-1,1);
   }
   return rs;
  }

  public static long Conv_XXto10(string xStr,int xBase)
  {
   string oStr = "0123456789ABCDEFGHJKLMNPQRSTUVWXY";
   int i,p;
   double rn = 0;
   string ch = "" ;
   for(i=xStr.Length; i>0; i--)
   {
    ch = xStr.Substring(i-1,1);
    p = oStr.IndexOf(ch);
    rn += p * Math.Pow(xBase,(xStr.Length-i));
   }
   return (long)rn;
  }
 }

 public class WebEmail 
 {
  public void SendEmail(string xSubj,string xCont,string FmAddr,string ToAddr)
  {
   //
   ////string MsgSubj;
   //
  }

  public void SendSmtp(string xSubj,string xCont,string xFrom,string xTo)
  {
   MailMessage mObj = new MailMessage();
   mObj.From    = xFrom;
   mObj.To      = xTo;
   mObj.Subject = xSubj;
   mObj.Body    = xCont;
   mObj.BodyFormat   = MailFormat.Html;
   //mObj.BodyEncoding = "gb2312";
   //mObj.Priority = MailPriority.Normal/Low/High;
   //mObj.Attachments.Add(new MailAttachment("c:\\test.txt"));
   SmtpMail.Send(mObj);
  }
 }

 public class WebFile
 {
  // File.Delete("F:\\net\\241.net\\t2\\t2\\t2\txt");
  // Directory.CreateDirectory("F:\\net\\241.net\\t2\\t2\\t2");  
  // Directory.Delete();
  public static string fCreate(string xFile,string xContent)
  {
   string fRes;
   FileStream fs;
   if ( !File.Exists(xFile) ) 
   {
    fs = new FileStream(xFile, FileMode.OpenOrCreate, FileAccess.Write);
    fRes = "Create"; 
   }
   else 
   {
    fs = new FileStream(xFile, FileMode.Truncate, FileAccess.Write);
    fRes = "Edit";
   }
   StreamWriter w = new StreamWriter(fs);
   w.Write (xContent);
   w.Flush();  
   fs.Close();
   return fRes;
  }
  public static string fRead(string xFile)
  {
   string fRes;
   if ( !File.Exists(xFile) ) { fRes = "\0"; }
   else 
   {
    FileStream fs = new FileStream(xFile, FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(fs); // ,Encoding.GetEncoding(950),true
    fRes = sr.ReadToEnd(); 
    fs.Close();
    
   }
   return fRes;
  }

  public static DataTable fList(string xPath)
  {
   Directory.CreateDirectory(xPath);
   DataTable dt = new DataTable();
   DataRow dr;
   dt.Columns.Add(new DataColumn("Object Name", typeof(string)));
   dt.Columns.Add(new DataColumn("Type", typeof(string)));
   dt.Columns.Add(new DataColumn("Size", typeof(string)));
   dt.Columns.Add(new DataColumn("Date Created", typeof(string)));
   dt.Columns.Add(new DataColumn("Date Modified", typeof(string)));

   DirectoryInfo cd = new DirectoryInfo(xPath+  "\\"); 
   foreach (DirectoryInfo Dir in cd.GetDirectories()) 
   {
    dr = dt.NewRow();  
    dr[0] = Dir.Name;
    dr[1] = "Dir.";
    dr[2] = "";  
    dr[3] = Dir.CreationTime.ToString("yy-MM-dd HH:mm");
    dr[4] = Dir.LastWriteTime.ToString("yy-MM-dd HH:mm");
    dt.Rows.Add(dr);  
   }
   foreach (FileInfo Fil in cd.GetFiles()) 
   {
    dr = dt.NewRow();
    dr[0] = Fil.Name;
    dr[1] = "File";
    dr[2] = Fil.Length; // "999,999,999,999"
    dr[3] = Fil.CreationTime.ToString("yy-MM-dd HH:mm");
    dr[4] = Fil.LastWriteTime.ToString("yy-MM-dd HH:mm");
    dt.Rows.Add(dr);      
   }   
   return dt;
  }

  public static ArrayList fUpload(HttpPostedFile xFile,string xPath,string xOrg,int xSize,string xType)
  {
   ArrayList OutArr = new ArrayList();
   OutArr.Add(""); //Rerurn Result 
   OutArr.Add(""); //Extend File Name
   OutArr.Add(""); //Org File Name
   OutArr.Add("0"); //Size
   OutArr.Add("0"); //Width
   OutArr.Add("0"); //Height
 
   HttpPostedFile pFile = xFile; // Get Files Info
   int fSize = pFile.ContentLength; 
   byte[] fData = new byte[fSize];
   string upFlag = "(OK)";
   string fType = "",fName="";

   xSize *= 1024;    // Check Files Size
   if( (xSize<5120)||(xSize>204800) ) { xSize = 198*1024; }
   if(fSize==0)    { upFlag = "Siz0"; }
   if(fSize>xSize) { upFlag = "Size"; }
   OutArr[3] = fSize.ToString(); //OutFileSize = fSize; // *****

   if(upFlag=="(OK)")   // Check Files Type
   {
    int cPos = xOrg.LastIndexOf("\\") + 1;
    fName = xOrg.Substring(cPos,(xOrg.Length-cPos));
    fType = fName;
    cPos = fType.LastIndexOf(".");
    if(cPos>0) { fType = fName.Substring(cPos,(fName.Length-cPos)).ToUpper(); }
    else  { fType = "(ER)"; }
    OutArr[1] = fType; //OutFileExt = fType;  // *****
    OutArr[2] = fName; //OutFileName = fName; // *****
    xType = xType.ToUpper();
    if(xType.IndexOf(fType)<0) { upFlag = "Type"; }
    string yType = "/.ASP/.ASPX/.EXE/.ASAX/.ASA/";
    if(yType.IndexOf(fType)>=0) { upFlag = "yTyp"; }
   }
 
   if(upFlag=="(OK)")  // Save Files
   {
    pFile.InputStream.Read(fData,0,fSize);
    xPath += fType;
    FileStream tFile = new FileStream(xPath,FileMode.Create);
    tFile.Write(fData,0,fData.Length);
    tFile.Close(); 
    if ( (fType==".GIF")||(fType==".JPG")||(fType==".JPEG") ) // Get Size
    {  
     FileStream fs = File.Open(xPath,FileMode.Open,FileAccess.Read,FileShare.Read);
     Bitmap tImg = new Bitmap(fs);
     OutArr[4] = tImg.Width.ToString(); //OutFileWidth = tImg.Width;  // *****
     OutArr[5] = tImg.Height.ToString(); //OutFileHeight = tImg.Height; // *****
     fs.Close();
    }
   }
   OutArr[0] = upFlag; //OutFileRes = upFlag;
   return OutArr;

  }

  public static string ImgShow(string xPName,int xImgW,int xImgH,int xMaxW,int xMaxH)
  {
   string fRes="";
   float oW=xMaxW, oH=xMaxH;
   float oScale=xMaxW/xMaxH;
   float iScale=xImgW/xImgW;
   if(( xImgW>xMaxW) && (xImgH>xMaxH) ) { // >>
    if(iScale>oScale) { oW = xMaxW; oH = xImgH/iScale; }
    if(iScale<oScale) { oH = xMaxH; oW = xMaxW*iScale; }
   }
   if(( xImgW>xMaxW) && (xImgH<xMaxH) ) { // ><
          oW = xMaxW; oH = xImgH/iScale;
   }
   if(( xImgW<xMaxW) && (xImgH>xMaxH) ) { // <>
          oH = xMaxH; oW = xMaxW*iScale;
   }
   if(( xImgW<xMaxW) && (xImgH<xMaxH) ) { // <<
          oW = xImgW; oH = xImgH; 
   }
   fRes = "<img border='0' src='" +xPName+ "' width=" +oW+ " height=" +oH+ ">";
   return fRes;
  }

  /**********************Created by Chen************************** 
  *如果你觉得本人的文章好,要引用请尊重著作人的劳动果实,说明 
  http://edu.pingjiang.com/net/02/45843.htm
  *出处以及原创作者,Thank you!!!   email:aishen944-sohu.com
  http://www.dg.gd.cn 东莞网络 谢永顺[Peace] 整理修改 (xpigeon@163.com;QQ:80893510)
  *******************************************************************/
  /// <summary>
  /// 创建验证码的图片 CreateValidateGraphic
  /// </summary>
  /// <param name="containsPage">要输出到的page对象</param>
  /// <param name="validateNum">验证码</param>
  public void ImgCode (Page containsPage,string validateNum)
  {
   Bitmap image=new Bitmap((int)Math.Ceiling(validateNum.Length*12.5),22);
   Graphics g=Graphics.FromImage(image);
   try
   {
    //生成随机生成器
    Random random=new Random();
    //清空图片背景色
    g.Clear(Color.White);
    //画图片的干扰线
    for(int i=0;i<5;i++)
    {
     int x1=random.Next(image.Width);
     int x2=random.Next(image.Width);
     int y1=random.Next(image.Height);
     int y2=random.Next(image.Height);
     g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);
    }//
    Font font=new Font("Courier",12,(FontStyle.Bold)); //Arial |FontStyle.Italic
    LinearGradientBrush brush=new LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),
     Color.Blue,Color.DarkRed,1.2f,true);
    g.DrawString(validateNum,font,brush,3,2);
    //画图片的前景干扰点
    for(int i=0;i<10;i++)
    {
     int x=random.Next(image.Width);
     int y=random.Next(image.Height);
     image.SetPixel(x,y,Color.FromArgb(random.Next()));
    }
    //画图片的边框线
    g.DrawRectangle(new Pen(Color.Silver),0,0,image.Width-1,image.Height-1);
    //保存图片数据
    MemoryStream stream=new MemoryStream();
    image.Save(stream,ImageFormat.Jpeg);
    //输出图片
    containsPage.Response.Clear();
    containsPage.Response.ContentType="image/jpeg";
    containsPage.Response.BinaryWrite(stream.ToArray());
   }
   finally
   {
    g.Dispose();
    image.Dispose();
   }
  }

 }
 // Get_rsOpt(xconn,xTab,xID,xName,xType,xWhere,xDef)  
 // Get_Option(xmid,xfirst,xend,xstep) ; 
 // Get_vPath(xLen) Get_fName()
}

分类: C# Asp.net

本文转自快乐就好博客园博客,原文链接:http://www.cnblogs.com/happyday56/archive/2007/11/01/946130.html,如需转载请自行联系原作者
上一篇:spring事务配置


下一篇:gitee命令操作总结--前端