- 博客(0)
- 资源 (9)
- 收藏
- 关注
c#发送邮件
public static void SendEmail()
{
try
{
if (DateTime.Now.Day % 2 == 0)
{
//实例化SmtpClient
SmtpClient client = new SmtpClient("smtp.126.com", 25);
//出站方式设置为NetWork
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtp服务器验证并制定账号密码
client.Credentials = new NetworkCredential("xxx@126.com", "123456");
MailMessage message = new MailMessage();
//设置优先级
message.Priority = MailPriority.Normal;
//设置收件方看到的邮件来源为:发送方邮件地址、来源标题、编码
message.From = new MailAddress("xxx@126.com", "2222", Encoding.GetEncoding("gb2312"));
//接收方
message.To.Add("dddddd@163.com");
//标题
message.Subject = "标题";
message.SubjectEncoding = Encoding.GetEncoding("gb2312");
//邮件正文是否支持HTML
message.IsBodyHtml = true;
//正文编码
message.BodyEncoding = Encoding.GetEncoding("gb2312");
string ip = new Regex(@"您的IP地址是:\[([\d.]+?)\]").Match
(new WebClient().DownloadString("http://www.ip138.com/ip2city.asp")).Groups[1].Value;
client.Send("<font color='red'>测试</font><br/>");
}
}
catch (Exception e)
{
}
}
2012-04-06
C# 建立快捷方式
private void CreateLink(string linkName, string ExeName,string Description)
{
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(linkName);
shortcut.TargetPath = ExeName;
shortcut.WorkingDirectory =Path.GetDirectoryName(ExeName);
shortcut.WindowStyle = 1;
shortcut.Description = Description;
shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32.dll, 165";
shortcut.Save();
}
private void button3_Click(object sender, EventArgs e)
{
CreateLink(Application.StartupPath + "/两权发证.lnk", @"D:\temp\安徽\安徽.mxd", "两权发证");
//WshShell shell = new WshShell();
//IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
// Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
// "\\" + "Allen's Application.lnk"
// );
//shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
//shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
//shortcut.WindowStyle = 1;
//shortcut.Description = "Launch Allen's Application";
//shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32.dll, 165";
//shortcut.Save();
}
2012-04-06
Revit API找到墙的正面
//找到墙的正面
public static Face FindWallFace(Wall wall)
{
Face normalFace = null;
//
Options opt = new Options();
opt.ComputeReferences = true;
opt.DetailLevel = Autodesk.Revit.DB.DetailLevels.Medium;
//
GeometryElement e = wall.get_Geometry(opt);
foreach (GeometryObject obj in e.Objects)
{
Solid solid = obj as Solid;
if (solid != null && solid.Faces.Size > 0)
{
foreach (Face face in solid.Faces)
{
PlanarFace pf = face as PlanarFace;
if (pf != null)
{
if (pf.Normal.AngleTo(wall.Orientation) < 0.01)//数值在0到PI之间
{
normalFace = face;
}
}
}
}
}
return normalFace;
}
2012-03-30
调用API
.NET调用新浪微博开放平台接口的代码示例 博客园在新浪微博上开了官方微博(http://t.sina.com.cn/cnblogs),为了方便一些信息的更新,比如IT新闻,我们使用了新浪微博开放平台接口。 在这篇文章中,我们将和大家分享如何通过.NET(C#)调用新浪微博开放平台接口。 使用新浪微博开放平台接口,需要先申请一帐号,申请方法:给 @微博开放平台 发送私信,或者给open_sina_mblog@vip.sina.com发邮件,附上您的email,微博个人主页,电话,和简单介绍。 我们发了申请邮件后,不到1小时就收到了申请通过的邮件。然后进入新浪微博开放平台查看相关文档,在文档中(使用Basic Auth进行用户验证)发现新浪微博开发团队推荐了园子里的Q.Lee.lulu写的一篇博文:访问需要HTTP Basic Authentication认证的资源的各种语言的实现。这篇文章成为了我们的重要参考,但该文只是针对“GET”请求的情况,而新浪微博开放平台接口要求HTTP请求方式为“POST”,我们又参考了园子里的乌生鱼汤写的另一篇博文: 使用HttpWebRequest发送自定义POST请求。在这里感谢两位园友的分享! 接下来,我们开始.NET调用新浪微博开放平台接口之旅。 1. 首先我们要获取一个App Key,在新浪微博开放平台的“我的应用”中创建一个应用,就会生成App Key,假设是123456。 2. 在新浪微博API文档中找到你想调用的API,这里我们假定调用发表微博的API-statuses/update,url是http://api.t.sina.com.cn/statuses/update.json,POST的参数:source=appkey&status;=微博内容。其中appkey就是之前获取的App Key。 3. 准备数据 1) 准备用户验证数据: string username = "t@cnblogs.com"; string password = "cnblogs.com"; string usernamePassword = username + ":" + password;复制代码 username是你的微博登录用户名,password是你的博客密码。 2) 准备调用的URL及需要POST的数据: string url = "http://api.t.sina.com.cn/statuses/update.json"; string news_title = "VS2010网剧合集:讲述程序员的爱情故事"; int news_id = 62747; string t_news = string.Format("{0},http://news.cnblogs.com/n/{1}/", news_title, news_id ); string data = "source=123456&status;=" + System.Web.HttpUtility.UrlEncode(t_news);复制代码4. 准备用于发起请求的HttpWebRequest对象: System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url); System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;复制代码5. 准备用于用户验证的凭据: System.Net.CredentialCache myCache = new System.Net.CredentialCache(); myCache.Add(new Uri(url), "Basic", new System.Net.NetworkCredential(username, password)); httpRequest.Credentials = myCache; httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(usernamePassword)));复制代码6. 发起POST请求: httpRequest.Method = "POST"; httpRequest.ContentType = "application/x-www-form-urlencoded"; System.Text.Encoding encoding = System.Text.Encoding.ASCII; byte[] bytesToPost = encoding.GetBytes(data); httpRequest.ContentLength = bytesToPost.Length; System.IO.Stream requestStream = httpRequest.GetRequestStream(); requestStream.Write(bytesToPost, 0, bytesToPost.Length); requestStream.Close();复制代码上述代码成功执行后,就会把内容发到了你的微博上了。 7. 获取服务端的响应内容: System.Net.WebResponse wr = httpRequest.GetResponse(); System.IO.Stream receiveStream = wr.GetResponseStream(); using (System.IO.StreamReader reader = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8)) { string responseContent = reader.ReadToEnd(); }复制代码好了,.NET调用新浪微博开放平台接口之旅就完成了,很简单吧。 如果没有Q.Lee.lulu与乌生鱼汤的文章作为参考,对我们来说就不会这么轻松,这也许就是分享的价值吧,你的一点小经验却可能给别人带来很大的帮助。 所以,我们也来分享一下,虽然不算什么经验,只是一个整理,但也许会对需要的人有帮助。
2012-03-30
Random伪随机数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace 伪随机数{ class Program { static void Main(string[] args) { Random rnd=new Random (); char c; Random ro = new Random(); Console.WriteLine("{0}", ro); int d = ro.Next(); Console.WriteLine("{0}",d); Random rc = new Random(); int f = rc.Next(); Console.WriteLine("{0}",f); //Thread.Sleep(10000); //Console.WriteLine("time later 10000"); Random s = new Random(); double cc = s.NextDouble(); Console.WriteLine("{0}",cc); int count = 4; Random[] rnds = new Random[count]; int[] ss = new int[4]; for (int i = 0; i < count; i++) { rnds[i] = new Random(unchecked((int)(DateTime.Now.Ticks >> i))); ss[i] = rnds[i].Next(); Console.WriteLine("{0}",ss[i]); } Random ww = new Random(); double ee = ww.NextDouble(); string gg = string.Format("{0:F5}", ee); Console.WriteLine("{0}",gg); int k = rc.Next(65, 91); c = (char)k; Console.WriteLine("{0}", c); string rr; rr = GetLetter(rnd); Console.WriteLine("{0}",rr); Console.ReadKey(); } public static string GetLetter(Random rnd) { // A-Z ASCII值 65-90 // a-z ASCII值 97-122 int i = rnd.Next(65, 123); char o = (char)i; if (char.IsLetter(o)) { return o.ToString(); } else { // 递归调用,直到随机到字母 return GetLetter(rnd); } } }}
2012-03-30
DataTable合并
//创建数据库连接
SqlConnection con = new SqlConnection("server=.;database = test; uid = sa; pwd = 123456");
try
{
//打开数据库连接
con.Open();
//数据适配器,传输数据库数据
SqlDataAdapter sda = new SqlDataAdapter("select * from Person where PersonId 150 and PersonId < 160", con);
DataSet ds = new DataSet();
sda.Fill(ds, "dt1");
sda1.Fill(ds, "dt2");
//DataTable1
DataTable dt1 = ds.Tables["dt1"];
//DataTable2
DataTable dt2 = ds.Tables["dt2"];
//将DataTable2中的行添加到DataTable1
//前提:dt1和dt2表结构相同
foreach (DataRow dr in dt2.Rows)
dt1.Rows.Add(dr.ItemArray);
//绑定表格
dataGridView1.DataSource = dt1;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
con.Close();
}
2012-03-30
取相对路径方法
//可获得当前执行的exe的文件名。 string str1 =Process.GetCurrentProcess().MainModule.FileName;// 获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。//备注:按照定义,如果该进程在本地或网络驱动器的根目录中启动,//则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:\”)。//如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如“C:\mySubDirectory”)。 string str2=Environment.CurrentDirectory;//获取应用程序的当前工作目录。 string str3=Directory.GetCurrentDirectory();//获取基目录,它由程序集冲突解决程序用来探测程序集。 string str4=AppDomain.CurrentDomain.BaseDirectory;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。 string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。 string str6=Application.ExecutablePath;//获取或设置包含该应用程序的目录的名称。string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase
2012-03-28
WinForm中实体类批量修改
在WinForm项目开发中习惯于对于集合数据的批量修改,再一次性提交更新同步到数据库。这里我们就必须实现对对象的改变的跟踪记录,我们实现对象的改变跟踪有许多方式,大致我尝试了两种方式:1:对象强制实现接口,State守信和MakeMark行为。2:利用字典序继续改变。虽然1的方式是否更加合理,但是在winform中与BindingSource集合使用简化修增修改的书写,配合的不是很好,供给开发人员使用不是很爽。于是我修改成为第二种方式集合记录更改,在继续在原集合真实修改,触发BindingSource事件和与BindingSource很好的结合。
2012-03-28
批处理格式转换可执行程序
能一键转换你的批处理文件到真实的程序文件 (.EXE 格式)。此程序可以在 Windows 2000/2003/XP/Vista 下无限制运行。一个 .EXE 文件更难随便反向工程,所以这可能是针对最终用户隐藏一个明显的批处理文件的一种方式。你的批处理文件的内容将被加密并保护被更改。
2011-08-18
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人