using System;
using System.Web.Security;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Web.Mail;
using System.Configuration;
using System.ComponentModel;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
namespace MY
{
/// <summary>
/// UserClass 的摘要说明。
/// </summary>
public class UserClass:System.Web.UI.Page
{
private string[] userdata;
private string[] managerdata;
private string strOutPicName;
private DbControl db;
protected Page page = null;
//private Hashtable ht;
public UserClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public UserClass(Page page)
{
db = new DbControl();
this.page = page;
strOutPicName = "";
}
#region 加密方法
/// <summary>
/// 加密
/// </summary>
/// <param name="pToEncrypt"></param>
/// <returns></returns>
public string Encrypt(string pToEncrypt)
{
string sKey="1234abcd";
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
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);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
public string Decrypt(string pToDecrypt)
{
string sKey="1234abcd";
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Put the input string into the byte array
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);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
#endregion
#region 处理字段落字符串
/// <summary>
/// 处理字段落字符串
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
public string Ctrltxt(string notes)
{
StringBuilder outstr=new StringBuilder(notes);
outstr.Replace("\n","<br>");
outstr.Replace(" "," ");
return outstr.ToString();
}
#endregion
#region 手动方法
/// <summary>
/// 从数据库里读取已经选 择的DR
/// </summary>
/// <param name="lb"></param>
/// <param name="strshow"></param>
/// <param name="intFlag"></param>
public void ShowDropDownList(DropDownList lb,string strshow,int intFlag)
{
foreach(ListItem objitem in lb.Items)
{
if (intFlag==0 && objitem.Text==strshow)
{
objitem.Selected=true;
}
else if (intFlag==1 && objitem.Value==strshow)
{
objitem.Selected=true;
}
else
{
objitem.Selected=false;
}
}
}
/// <summary>
/// 把数据里的值读到DL里面。
/// </summary>
/// <param name="dl"></param>
/// <param name="dr"></param>
/// <param name="strtext"></param>
/// <param name="strvalue"></param>
public void ShowDrData(DropDownList dl,SqlDataReader dr,string strtext,string strvalue)
{
dl.DataSource=dr;
dl.DataTextField=strtext;
dl.DataValueField=strvalue;
dl.DataBind();
}
/// <summary>
/// 把数据里的值读到DL里面并且选择某一项为选中,重载上面的!
/// </summary>
/// <param name="dl"></param>
/// <param name="dr"></param>
/// <param name="strtext"></param>
/// <param name="strvalue"></param>
/// <param name="sltvalue"></param>
public void ShowDrData(DropDownList dl,SqlDataReader dr ,string strtext,string strvalue,string sltvalue)
{
dl.DataSource=dr;
dl.DataTextField=strtext;
dl.DataValueField=strvalue;
dl.DataBind();
dl.Items.Add(sltvalue);
foreach(ListItem objitem in dl.Items)
{
if (objitem.Text==sltvalue)
{
objitem.Selected=true;
}
else
{
objitem.Selected=false;
}
}
}
/// <summary>
/// 重载上面的函数将Reader参数换成Array
/// </summary>
/// <param name="dl"></param>
/// <param name="al"></param>
public void ShowDrData(DropDownList dl,ArrayList al)
{
dl.DataSource=al;
//dl.DataTextField=strtext;
//dl.DataValueField=strvalue;
dl.DataBind();
}
/// <summary>
/// 把数据库里的数据读到LB里面去.
/// </summary>
/// <param name="lb"></param>
/// <param name="dr"></param>
/// <param name="strtext"></param>
/// <param name="strvalue"></param>
public void ShowDrData(ListBox lb,SqlDataReader dr,string strtext,string strvalue)
{
lb.DataSource=dr;
lb.DataTextField=strtext;
lb.DataValueField=strvalue;
lb.DataBind();
}
/// <summary>
/// 将数据读到Array数组//
/// </summary>
/// <param name="dr"></param>
/// <param name="strslt"></param>
/// <param name="strfield"></param>
/// <returns></returns>
public ArrayList ReturnArray(SqlDataReader dr,string strslt,string strfield)
{
ArrayList objarr;
objarr=new ArrayList();
if (dr.HasRows)
{
while (dr.Read())
{
objarr.Add(dr[strfield].ToString());
}
dr.Close();
}
objarr.Insert(0,strslt);
return objarr;
}
public void Close()
{
db.Close();
userdata=null;
}
/// <summary>
/// 读取Check组的值;
/// </summary>
/// <param name="lb"></param>
/// <returns></returns>
public string ReadCheckBoxList(CheckBoxList lb)
{
string returnvalue="0";
for (int i=0; i<lb.Items.Count; i++)
{
if (lb.Items[i].Selected)
returnvalue=returnvalue +","+ lb.Items[i].Value;
}
return returnvalue;
}
/// <summary>
/// 显示Check组的值
/// </summary>
/// <param name="cbl"></param>
/// <param name="strCheckValue"></param>
public void ShowCheckBoxList(CheckBoxList cbl,string strCheckValue)
{
for (int i=0;i<cbl.Items.Count;i++)
{
if (strCheckValue.IndexOf(cbl.Items[i].Value)>0)
{
cbl.Items[i].Selected=true;
}
}
}
/// <summary>
/// DateDiff功能实现函数
/// </summary>
/// <param name="howtocompare"></param>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
public double DateDiff(string howtocompare, System.DateTime startDate, System.DateTime endDate)
{
double diff=0;
System.TimeSpan TS = new System.TimeSpan(startDate.Ticks-endDate.Ticks);
switch (howtocompare.ToLower())
{
case "m":
diff = Convert.ToDouble(TS.TotalMinutes);
break;
case "s":
diff = Convert.ToDouble(TS.TotalSeconds);
break;
case "t":
diff = Convert.ToDouble(TS.Ticks);
break;
case "mm":
diff = Convert.ToDouble(TS.TotalMilliseconds);
break;
case "yyyy":
diff = Convert.ToDouble(TS.TotalDays/365);
break;
case "q":
diff = Convert.ToDouble((TS.TotalDays/365)/4);
break;
default:
//d
diff = Convert.ToDouble(TS.TotalDays);
break;
}
return diff;
}
/// <summary>
/// 以送Email过程.
/// </summary>
/// <param name="strMailTo"></param>
/// <param name="strMailSubject"></param>
/// <param name="strMailBody"></param>
/// <returns></returns>
public bool sendMail(string strMailTo,string strMailSubject,string strMailBody )
{
try
{
string MailSmtp=ConfigurationSettings.AppSettings["MailSmtp"];
string MailUser=ConfigurationSettings.AppSettings["MailUser"];
string MailPassword=ConfigurationSettings.AppSettings["MailPassword"];
string MailFrom=ConfigurationSettings.AppSettings["MailFrom"];
MailMessage mailMessage = new MailMessage();
mailMessage.To = "zxqd2008@163.com";
mailMessage.From = MailFrom;
mailMessage.Subject = strMailSubject;
mailMessage.Body = strMailBody;
mailMessage.BodyFormat = MailFormat.Html;//邮件的格式
//如果带有附件...如:attachFile =''C:\file.rar''
//if (attachFile != "")
//{
//mailMessage.Attachments.Add(new MailAttachment(attachFile));
//}
//以下三句用在SMTP需要身份验证时。Framework1.1才有支持。
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//基本验证
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailUser); //设定用户名
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailPassword);//设定密码
SmtpMail.SmtpServer = MailSmtp;//设定SMTP服务器
SmtpMail.Send(mailMessage);
return true;
}
catch
{
//邮件发送失败处理
return false;
//throw ex;
}
}
//============================================================
/// <summary>
/// 将一特定的值绑定到Panel中的CheckBox里(根据CheckBox里的Value值判断此项是否选中)
/// </summary>
/// <param name="strValue"></param>
/// <param name="objPanel"></param>
public void showPanelCheckBox(string strValue,Panel objPanel)
{
for (int i=0;i<=objPanel.Controls.Count-1;i++)
{
if (objPanel.Controls[i] is CheckBox)
{
//if (this.Panel1.Controls[i])
CheckBox cb=(CheckBox)objPanel.Controls[i];
if (strValue.IndexOf(cb.ID.Substring(1,cb.ID.Length-1))>0)
{
cb.Checked=true;
}
else
{
cb.Checked=false;
}
}
}
}
//============================================================
//上传图片时取得图片的名字
/// <summary>
/// 缺点多等待处理
/// </summary>
/// <param name="uploadPic"></param>
/// <param name="strFileSavePath"></param>
/// <returns></returns>
public bool outPutPicName(System.Web.UI.HtmlControls.HtmlInputFile uploadPic,string strFileSavePath)
{
//string strPicName = uploadPic.Value.Length.ToString();
strOutPicName = "";
SqlDataReader sdr = db.SqlGetReader("Select * From Data_Counter Where isBy = '1'");
sdr.Read();
string picFileName = sdr["iSum"].ToString();
sdr.Close();
int piclen = uploadPic.Value.Length;
int picwei = uploadPic.Value.LastIndexOf(".");
string picname = uploadPic.Value.Substring(picwei+1,piclen-picwei-1);
picname = picname.ToLower();
if (picname!="jpg"&&picname!="jpeg"&&picname!="gif"&&picname!="bmp")
{
strOutPicName = db.RetrunScript("what you uploaded can only be GIF , JPEG or JPG format...",2);
return false;
}
else
{
strOutPicName = picFileName+"."+picname;
//更新计数器 执行sql语句
return true;
}
}
public string OutPicName
{
get
{
return strOutPicName;
}
}
/// <summary>
/// 选中有值的选项
/// </summary>
/// <param name="objrbl"></param>
/// <param name="strValue"></param>
public void ShowRadioList(RadioButtonList objrbl, string strValue)
{
for(int i = 0;i<=objrbl.Items.Count-1;i++)
{
if (strValue.IndexOf(objrbl.Items[i].Value)!=-1)
{
objrbl.Items[i].Selected = true;
}
else
{
objrbl.Items[i].Selected = false;
}
}
}
#endregion
/// <summary>
/// 图像缩小处理
/// </summary>
/// <param name="strBigImg">存储原图像路径及文件名</param>
/// <param name="strSmallImg">存储缩小后图像路径及文件名</param>
/// <param name="intW">存储图像缩小宽度</param>
/// <param name="intH">存储图像缩小高度</param>
/// <param name="objPage">当前页</param>
/// <returns>缩小后图像路么及文件名</returns>
public string imgControl(Page objPage,string strBigImg,string strSmallImg,int intW,int intH)
{
if (File.Exists(strSmallImg))
{
//检验图像是否已经缩小
return strSmallImg;
}
else
{
//图像没有缩小时处理
Bitmap oldImg;//原图对象
Bitmap newImg;//新图对象
try
{
oldImg = new Bitmap(objPage.MapPath(strBigImg));
newImg = new Bitmap(oldImg,intW,intH);
newImg.Save(objPage.MapPath(strSmallImg));
oldImg.Dispose();
newImg.Dispose();
//strSmallImg = "系统默认出错图片的路径与名称";
}
catch
{
}
finally
{
oldImg = null;
newImg = null;
}
strSmallImg = strSmallImg.Trim();
return strSmallImg;
}
}
}
}