public static bool CheckIsNumber(string str)
{
Regex objNotNumberPattern=new Regex("[^0-9.-]");
Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(str) &&
!objTwoDotPattern.IsMatch(str) &&
!objTwoMinusPattern.IsMatch(str) &&
objNumberPattern.IsMatch(str);
}
public bool CheckIsDate(string p_strDate)
{
Regex Pattern=new Regex("(\\d{4}-)?[0-1]?\\d{1}-[0-3]?\\d{1}");
return Pattern.IsMatch(p_strDate) ;
}
public bool IsNumber(string str)
{
Regex Pattern=new Regex("[0-9]");
return Pattern.IsMatch(str) ;
}
public void CreateFile(string filename,string content)//创建文件
{
try
{
using (StreamWriter sw = new StreamWriter (filename,false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.Write (content);
sw.Close();
}
}
catch(Exception e)
{
throw new Exception(e.ToString());
}
finally
{
}
}
public bool Exists(string filepath)
{
if(File.Exists(filepath))
{
return true;
}
else
{
return false;
}
}
public string ReadFile(string filename)//读取文件
{
try
{
using (StreamReader sr = new StreamReader(filename,System.Text.Encoding.GetEncoding("GB2312")))
{
string temp=sr.ReadToEnd();
sr.Close();
return temp;
}
}
catch(Exception e)
{
throw new Exception(e.ToString());;
}
finally
{
}
}
public static string GetRandNum(int randNumLength)//取得指定长度随机数
{
System.Random randNum = new System.Random(unchecked((int) DateTime.Now.Ticks));
StringBuilder sb = new StringBuilder(randNumLength);
for ( int i = 0; i < randNumLength; i++ )
{
sb.Append( randNum.Next( 0, 9 ));
}
return sb.ToString();
}
public string GetFileName()//取得新文件名
{
return System.DateTime.Now.ToString("yyyyMMddHHmmss")+GetRandNum(2);
}
public string GetExtendName(string p_str)//取得新文件名
{
if(p_str!="")
return p_str.Substring(p_str.LastIndexOf("."));//取得文件扩展名
else
return "";
}
public string SubStr(string strInput,int intLen,string Str2)//截取字符串
{
strInput=strInput.Trim();
byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);
if (myByte.Length>intLen)
{
//截取操作
string resultStr="";
for(int i=0;i<strInput.Length;i++)
{
byte[] tempByte=System.Text.Encoding.Default.GetBytes(resultStr);
if(tempByte.Length<intLen)
{
resultStr+=strInput.Substring(i,1);
}
else
{
break;
}
}
return resultStr+Str2;
}
else
{
return strInput;
}
}
// public string SubStr(string Str,int Count,string Str2)//截取字符串
// {
// string temp;
// if (Count>Str.Length)
// temp=Str.Substring(0,Str.Length);
// else
// temp=Str.Substring(0,Count)+Str2;
// return temp;
// }
public string SubStr(string Str,int Count,int type)//截取字符串
{
string temp="";
if (Count>Str.Length)
temp=Str.Substring(0,Str.Length);
else
{
if (type==1)
temp=Str.Substring(0,Count);
}
return temp;
}
//上传图片并生成缩略图
public string UpLoadImage(Stream upimgfile,string OldPath,string NewPath,int Width,int Height)
{
System.Drawing.Image oldimg,newimg;
oldimg=System.Drawing.Image.FromStream(upimgfile);
System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
int newwidth,newheight;
newwidth=Width;
newheight=Height;
OldPath=OldPath.ToLower();
System.Drawing.Imaging.ImageFormat type=OldPath.EndsWith("gif")?System.Drawing.Imaging.ImageFormat.Gif:System.Drawing.Imaging.ImageFormat.Jpeg;
string type2=OldPath.EndsWith("gif")?".gif":".jpg";
string filename=this.GetFileName()+type2;
newimg=oldimg.GetThumbnailImage(newwidth,newheight,callb, new System.IntPtr(0));
newimg.Save(Server.MapPath(NewPath.ToLower()+filename),type);
oldimg.Dispose();
newimg.Dispose();
return filename;
}
public string UpLoadImage(Stream upimgfile,string OldPath,string NewPath,string PicName,int Width,int Height)
{
System.Drawing.Image oldimg,newimg;
oldimg=System.Drawing.Image.FromStream(upimgfile);
System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
int newwidth,newheight;
newwidth=Width;
newheight=Height;
OldPath=OldPath.ToLower();
System.Drawing.Imaging.ImageFormat type=OldPath.EndsWith("gif")?System.Drawing.Imaging.ImageFormat.Gif:System.Drawing.Imaging.ImageFormat.Jpeg;
string type2=OldPath.EndsWith("gif")?".gif":".jpg";
if(PicName.LastIndexOf(".")>0)
PicName=PicName.Substring(0,PicName.LastIndexOf("."));
string filename=PicName+type2;
newimg=oldimg.GetThumbnailImage(newwidth,newheight,callb, new System.IntPtr(0));
newimg.Save(Server.MapPath(NewPath.ToLower()+filename),type);
oldimg.Dispose();
newimg.Dispose();
return filename;
}
public void CompressImage(string p_OldPath,string p_NewPath,int p_Width,int p_Height)
{
System.Drawing.Image oldimg,newimg;
oldimg=System.Drawing.Image.FromFile(p_OldPath);
System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
p_OldPath=p_OldPath.ToLower();
System.Drawing.Imaging.ImageFormat type=p_OldPath.EndsWith("gif")?System.Drawing.Imaging.ImageFormat.Gif:System.Drawing.Imaging.ImageFormat.Jpeg;
newimg=oldimg.GetThumbnailImage(p_Width,p_Height,callb, new System.IntPtr(0));
oldimg.Dispose();
newimg.Save(p_NewPath.ToLower(),type);
newimg.Dispose();
}
public bool ThumbnailCallback()
{
return false;
}
public void ListToTextBox(System.Web.UI.WebControls.DropDownList ddl,ref System.Web.UI.WebControls.TextBox txt,int type)//向文本框输出选择信息
{
switch(type)
{
case 0:
txt.Text=ddl.SelectedValue;
break;
case 1:
if(ddl.SelectedValue!="")
{
string temp="/"+ txt.Text.Trim()+"/";
if(temp.IndexOf("/"+ddl.SelectedValue+"/")<0)
if (txt.Text=="")
txt.Text=ddl.SelectedValue;
else
txt.Text+="/"+ddl.SelectedValue;
}
break;
}
}
//获取http页面函数
public string Get_Http(string a_strUrl)
{
string strResult="" ;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(a_strUrl) ;
try
{
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream myStream = res.GetResponseStream() ;
- 1
- 2
- 3
- 4
- 5
前往页