c#常用代码(转)

1.如何在ASP.NET中获得客户端IP地址
Request.UserHostAddress

2.如何取得一定范围内的随机数 

//创建随机数对象
Random rnd = new Random();
//调用Next方法产生随机数
txtRnd.Text = rnd.Next(1,20).ToString();

 

3.如何取得文件的扩展名示例4.如何取得文件的扩展名示例

//取得指定的文件路径
string path = txtExtName.Text.Trim();
//txtExtName.Text = Path.GetExtension(path);
//取得指定路径中文件的扩展名
txtExtName.Text = path. Substring(path.LastIndexOf(".")+1); 

 

txtExtName.Text = Path.GetExtension(selFile.PostedFile.FileName);

 

5.如何把字符串型数据转换为整型
try
{
 //取得输入的数字
 int strNum = Convert.ToInt32(txtStrNum.Text.Trim());
 //把输入的数字扩大10倍
 int NewNum = strNum * 10;
 //显示新的数据
 txtStrNum.Text = NewNum.ToString();
}
catch
{
 Response.Write("请输入数字");

 

6、得到系统当前日期

DateTime.Now.ToShortDateString();

7. 得到系统时间

txtCurTime.Text = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" +DateTime.Now.Day.ToString() + " "
    + DateTime.Now.Hour.ToString() + ":" +DateTime.Now.Minute.ToString() + ":" +DateTime.Now.Second.ToString();

8.取当前系统中的年

txtYear.Text = DateTime.Now.Year.ToString();

9.如何取得客户端的鼠标坐标并反馈到服务器端

<script language="javascript">
  function document.onmousemove()
        {
          document.all("txtbox").value = event.screenX + "x" + event.screenY;
        }
  </script>

再将值传回服务器端。

10.判断星期几

string dt ,week="";

   dt = DateTime.Now.DayOfWeek.ToString();
   switch  (dt)
   {
    case "Monday":
     week= "星期一";
     break;
    case "Tuesday" :
     week= "星期二";
     break;
    case "Wednesday":
     week= "星期三";
     break;
    case "Thursday" :
     week= "星期四";
     break;
    case "Friday" :
     week= "星期五";
     break;
    case "Saturday":
     week= "星期六";
     break;
    case "Sunday":
     week = "星期日";
     break;

   }
   txtWeek.Text =  week;

11.判断指定年的总天数

//判断是否是闰年
   if(DateTime.IsLeapYear(Convert.ToInt32(txtYearDay.Text)))
   {
    //如果是闰年
    txtYearDay.Text = "366天";
   }
   else
   {
    //如果不是闰年
    txtYearDay.Text = "365天";
   }

12.当前月天数

int year,month,days=0;
   year = DateTime.Now.Year ;
   month = DateTime.Now.Month ;

   //得到当前月的天数信息。
   switch (month)
   {
    case 1:
     days = 31;
     break;
    case 2:
     if (DateTime.IsLeapYear(year))
     {
      //闰年二月为 29 天
      days = 29;
     }
     else
     {
      //闰年二月为 28 天
      days = 28;
     }

     break;
    case 3:
     days = 31;
     break;
    case 4:
     days = 30;
     break;
    case 5:
     days = 31;
     break;
    case 6:
     days = 30;
     break;
    case 7:
     days = 31;
     break;
    case 8:
     days = 31;
     break;
    case 9:
     days = 30;
     break;
    case 10:
     days = 31;
     break;
    case 11:
     days = 30;
     break;
    case 12:
     days = 31;
     break;
   }   

   txtMonthDays.Text = days.ToString();

13.如何使用JMAIL组件实现邮件的发送

jmail.MessageClass oJmailMessage = new jmail.MessageClass();//建立发邮件类

   oJmailMessage.Charset = "GB2312";// 字符集
   oJmailMessage.Encoding = "BASE64";//附件的编码格式
   oJmailMessage.ContentType = "text/html";
   oJmailMessage.ISOEncodeHeaders = false;//是否将信头编码成iso-8859-1字符集
   oJmailMessage.Priority = Convert.ToByte(1);// 优先级
  
   oJmailMessage.From = TxtEmail.Text.Trim();//发送人邮件地址
   oJmailMessage.FromName = TxtName.Text.Trim();//发送人姓名
   oJmailMessage.Subject = txtSubject.Text.Trim();// 邮件主题
   oJmailMessage.MailServerUserName = TxtSmtpUser.Text.Trim();//身份验证的用户名
   oJmailMessage.MailServerPassWord = TxtSmtpPwd.Text.Trim();//用户密码

   //添加一个收件人,抄送人和密送人的添加和该方法是一样的,只是分别使用AddRecipientCC和RecipientBCC两个属性
   //要是需要添加多个收件人,则重复下面的语句即可。添加多个抄送和密送人的方法一样
   oJmailMessage.AddRecipient(txtReciver.Text.Trim(),"","");
   if("" != upFile.PostedFile.FileName)
   {
    string attpath = upFile.PostedFile.FileName;
    oJmailMessage.AddAttachment(@attpath,true,attpath.Substring(attpath.LastIndexOf(".")+1,3));//添加附件
   }
   oJmailMessage.Body = txtContent.Text.Trim();//邮件内容

   if(oJmailMessage.Send(TxtSmtServer.Text.Trim(),false))
   {
    Response.Write("发送成功!");
   }
   else
   {
    Response.Write("发送失败,请检查邮件服务器的设置!");
   }

   oJmailMessage = null;

14..如何使用JMAIL组件实现邮件的接收

jmail.POP3Class popMail = new POP3Class();//建立收邮件对象
   jmail.Message mailMessage;  //建立邮件信息接口
   jmail.Attachments atts;//建立附件集接口
   jmail.Attachment att;//建立附件接口

   try
   {
    popMail.Connect(TxtPopUser.Text.Trim(),TxtPopPwd.Text.Trim(),TxtPopServer.Text.Trim(),Convert.ToInt32(TxtPopPort.Text.Trim()));

    if(0 < popMail.Count)                                                                          //如果收到邮件
    {
     for(int i=1;i <= popMail.Count;i++)                                                         //根据取到的邮件数量依次取得每封邮件
     {
      mailMessage = popMail.Messages[i];                                                        //取得一条邮件信息
      atts = mailMessage.Attachments;                                                           //取得该邮件的附件集合
      mailMessage.Charset = "GB2312";                                                           //设置邮件的编码方式
      mailMessage.Encoding = "Base64";                                                          //设置邮件的附件编码方式
      mailMessage.ISOEncodeHeaders = false;                                                     //是否将信头编码成iso-8859-1字符集
      txtpriority.Text = mailMessage.Priority.ToString();                                       //邮件的优先级                
      txtSendMail.Text = mailMessage.From;                                                      //邮件的发送人的信箱地址
      txtSender.Text = mailMessage.FromName;                                                    //邮件的发送人
      txtSubject.Text = mailMessage.Subject;                                                    //邮件主题
      txtBody.Text = mailMessage.Body;                                                          //邮件内容
      txtSize.Text = mailMessage.Size.ToString();                                                          //邮件大小
      
      for(int j=0;j<atts.Count;j++)
      {
       att = atts[j];                                                                        //取得附件
       string attname = att.Name;                                                            //附件名称
       
       att.SaveToFile("e:\\attFile\\"+attname);                                              //上传到服务器
       
      }
      
     }
     panMailInfo.Visible = true;
     att = null;
     atts = null;
    }
    else
    {
     Response.Write("没有新邮件!");
    }

    popMail.DeleteMessages();
    popMail.Disconnect();
    popMail = null;
   }
   catch
   {
    Response.Write("Warning!请检查邮件服务器的设置是否正确!");
   }

posted @ 2009-02-28 15:21  Devil_Zhang  阅读(361)  评论(0编辑  收藏  举报