首页logo
  •  

jonllen

金龙,目前就职于一家软件公司,从事Java和.Net信息安全开发设计。

个人档案

jonllen
心情闪存 | 给他留言
妮称:jonllen
来自:中国. 湖南. 湘潭
简述:金龙,目前就职于一家软件公司,从事Java和.Net信息安全开发设计。
博客日历

asp.net(C#)常用的一些功能

分类:Asp.Net

发送邮件

public void SendEmail(string mailTo, string subject, string body)
{
MailMessage mail
= new MailMessage(this.EmailFrom, mailTo, subject, body);
mail.IsBodyHtml
= true;

SmtpClient smtp
= new SmtpClient();
smtp.Host
= this.SmtpHost;
smtp.Port
= this.SmtpPort;
smtp.EnableSsl
= this.SmtpSsl;

smtp.Credentials
= new NetworkCredential(this.EmailAccount, this.EmailPassword);

smtp.Send(mail);

}

直接读取上传文件内容(不经保存)

if (uploadFile.HasFile)
{
//读取导入帐本文件
int fileLength = uploadFile.PostedFile.ContentLength;
byte[] input = new byte[fileLength];
System.IO.Stream stream
= uploadFile.PostedFile.InputStream;
stream.Read(input,
0, fileLength);
stream.Position
= 0;
System.IO.StreamReader reader
= new System.IO.StreamReader(stream, System.Text.Encoding.Default);
string txtContent = reader.ReadToEnd();
reader.Close();
stream.Close();

//保存在服务器端
System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath(filePath), false);
writer.Write(txtContent);
writer.Close();
}

使用匿名委托筛选泛型列表和排序

List<SH_Payment_bean> PaymentList = new List<SH_Payment_bean>();

//使用匿名委托删除所筛选列表
PaymentList.RemoveAll(
delegate(SH_Payment_bean bean)
{
bean.CCBPayment
= new CCBPayment();
return string.IsNullOrEmpty(bean.fdPaymNO);
}
);

//按fdPaymID排序
PaymentList.Sort(
delegate(SH_Payment_bean x, SH_Payment_bean y)
{
if (x.fdPaymID < y.fdPaymID)
{
return -1;
}
else if (x.fdPaymID == y.fdPaymID)
{
return 0;
}
else return 1;
}
);

ASP.Net(C#)导出csv报表文件

StringBuilder sb = new StringBuilder("商品名称,交易金额,姓名,下单人姓名/帐号,数量,交易日期");
foreach (SH_Order_bean bean in list){
sb.AppendLine();
sb.AppendFormat(
"{0},{1},{2},{3},{4},{5}"
, bean.Item0.Goods.fdGoodName
, bean.fdOrdePayPrice.ToString(
"c").Replace(",", "")
, bean.fdOrdeUserName.Replace(
",", "")
, (bean.Member.fdMembLoginID
+ "/" + bean.Member.fdMembName).Replace(",", "")
, bean.Item0.fdItemQuantity
, bean.fdOrdePayAt.ToString(
"yyyy-MM-dd hh:mm")
);
}
Response.ClearHeaders();
Response.AppendHeader(
"Content-disposition", "attachment;filename=ExportData.csv");
Response.Write(sb.ToString());
Response.End();

volatile同步单件类

private static object lockHelper = new object();
private static volatile SmtpAgent instance = null;

public static SmtpAgent GetAgent()
{
if (instance == null)
{
lock (lockHelper)
{
if (instance == null)
{
instance
= new SmtpAgent();
}
}
}
return instance;

}

public static void SetInstance(SmtpAgent anInstance)
{
if (anInstance != null)
instance
= anInstance;
}

public static void SetInstance()
{
SetInstance(
new SmtpAgent());
}

序列化当前实体对象

XmlSerializer xmlFormat = new XmlSerializer(this.GetType());

FileStream fStream
= new FileStream(this.configPath, FileMode.Create, FileAccess.Write, FileShare.None);

xmlFormat.Serialize(fStream,
this);

fStream.Close();
标签:Asp.Net
  • posted@ 2009-12-04 11:50
  • update@ 2010-04-06 17:48:08
  • 阅读(8247)
  • 评论(0)

相关文章

评论
暂无任何评论。
发表评论
*必填
回复通知我
*必填

博文推荐