C sharp

来自OSSmedia

C#

实现发送电子邮件

using System.Net.Mail;

try

           { 
           MailMessage mail = new MailMessage(); 
           string from = "XXXXXX@163.com";   发件人地址 
           string fromname = "your name"; 
           mail.From = new MailAddress(from, fromname); 
           string to = "XXXXXXX@qq.com"; 收件人 
           mail.To.Add(to); 
           string subject = "测试邮件"; 
           mail.Subject = subject; 
           mail.BodyEncoding = System.Text.Encoding.UTF8; 
           mail.Priority = MailPriority.Normal; 
           string body = "这时测试邮件
测试html格式"; mail.Body = body; mail.IsBodyHtml = true; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; string fujian = Server.MapPath("//") +"XXXXX.rar"; if (fujian.Length > 0) { mail.Attachments.Add(new Attachment(fujian)); } SmtpClient smtp = new SmtpClient("smtp.163.com", 25); smtp.UseDefaultCredentials = true; smtp.UseDefaultCredentials = false; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //指定登录名和密码 smtp.Credentials = new System.Net.NetworkCredential("loginname", "password"); smtp.Timeout = 10000; smtp.Send(mail); Label2.Text = "send ok"; } catch (Exception exp) { Label2.Text = exp.Message; }

using System.Net.Mail; public bool SendMailUse(string sendTo, string subject, string body)

       { 
           string host = "smtp.163.com";// 邮件服务器smtp.163.com表示网易邮箱服务器     
           string userName = "xxxxxxxx@163.com";// 发送端账号    
           string password = "xxxxxxx";// 发送端密码(这个客户端重置后的密码) 
           SmtpClient client = new SmtpClient(); 
           client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式     
           client.Host = host;//邮件服务器 
           client.UseDefaultCredentials = true; 
           client.Credentials = new System.Net.NetworkCredential(userName, password);//用户名、密码 
           string strfrom = userName; 
           //string strcc = "xxxxxxxx@qq.com";//抄送 
           System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 
           msg.From = new MailAddress(strfrom, "盛铭逸品"); 
           msg.To.Add(sendTo); 
           //msg.CC.Add(strcc);  //抄送 
 
           msg.Subject = subject;//邮件标题    
           msg.Body = body;//邮件内容    
           msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
           msg.IsBodyHtml = true;//是否是HTML邮件    
           msg.Priority = MailPriority.High;//邮件优先级    
 
 
           try 
           { 
               client.Send(msg); 
               return true; 
           } 
           catch (System.Net.Mail.SmtpException ex) 
           { 
               return false; 
           } 
       } 

小写数字转换成大写金额

public string MoneyToChinese(string LowerMoney)

       { 
 
           string functionReturnValue = null; 
 
           bool IsNegative = false; // 是否是负数 
 
           if (LowerMoney.Trim().Substring(0, 1) == "-") 
           { 
 
               // 是负数则先转为正数 
 
               LowerMoney = LowerMoney.Trim().Remove(0, 1); 
 
               IsNegative = true; 
 
           } 
 
           string strLower = null; 
 
           string strUpart = null; 
 
           string strUpper = null; 
 
           int iTemp = 0; 
 
           // 保留两位小数 123.489→123.49  123.4→123.4 
 
           LowerMoney = Math.Round(double.Parse(LowerMoney), 2).ToString(); 
 
           if (LowerMoney.IndexOf(".") > 0) 
           { 
 
               if (LowerMoney.IndexOf(".") == LowerMoney.Length - 2) 
               { 
 
                   LowerMoney = LowerMoney + "0"; 
 
               } 
 
           } 
 
           else 
           { 
 
               LowerMoney = LowerMoney + ".00"; 
 
           } 
 
           strLower = LowerMoney; 
 
           iTemp = 1; 
 
           strUpper = ""; 
 
           while (iTemp <= strLower.Length) 
           { 
 
               switch (strLower.Substring(strLower.Length - iTemp, 1)) 
               { 
 
                   case ".": 
 
                       strUpart = "圆"; 
 
                       break; 
 
                   case "0": 
 
                       strUpart = "零"; 
 
                       break; 
 
                   case "1": 
 
                       strUpart = "壹"; 
 
                       break; 
 
                   case "2": 
 
                       strUpart = "贰"; 
 
                       break; 
 
                   case "3": 
 
                       strUpart = "叁"; 
 
                       break; 
 
                   case "4": 
 
                       strUpart = "肆"; 
 
                       break; 
 
                   case "5": 
 
                       strUpart = "伍"; 
 
                       break; 
 
                   case "6": 
 
                       strUpart = "陆"; 
 
                       break; 
 
                   case "7": 
 
                       strUpart = "柒"; 
 
                       break; 
 
                   case "8": 
 
                       strUpart = "捌"; 
 
                       break; 
 
                   case "9": 
 
                       strUpart = "玖"; 
 
                       break; 
 
               } 
 
 
 
               switch (iTemp) 
               { 
 
                   case 1: 
 
                       strUpart = strUpart + "分"; 
 
                       break; 
 
                   case 2: 
 
                       strUpart = strUpart + "角"; 
 
                       break; 
 
                   case 3: 
 
                       strUpart = strUpart + ""; 
 
                       break; 
 
                   case 4: 
 
                       strUpart = strUpart + ""; 
 
                       break; 
 
                   case 5: 
 
                       strUpart = strUpart + "拾"; 
 
                       break; 
 
                   case 6: 
 
                       strUpart = strUpart + "佰"; 
 
                       break; 
 
                   case 7: 
 
                       strUpart = strUpart + "仟"; 
 
                       break; 
 
                   case 8: 
 
                       strUpart = strUpart + "万"; 
 
                       break; 
 
                   case 9: 
 
                       strUpart = strUpart + "拾"; 
 
                       break; 
 
                   case 10: 
 
                       strUpart = strUpart + "佰"; 
 
                       break; 
 
                   case 11: 
 
                       strUpart = strUpart + "仟"; 
 
                       break; 
 
                   case 12: 
 
                       strUpart = strUpart + "亿"; 
 
                       break; 
 
                   case 13: 
 
                       strUpart = strUpart + "拾"; 
 
                       break; 
 
                   case 14: 
 
                       strUpart = strUpart + "佰"; 
 
                       break; 
 
                   case 15: 
 
                       strUpart = strUpart + "仟"; 
 
                       break; 
 
                   case 16: 
 
                       strUpart = strUpart + "万"; 
 
                       break; 
 
                   default: 
 
                       strUpart = strUpart + ""; 
 
                       break; 
 
               } 
 
 
 
               strUpper = strUpart + strUpper; 
 
               iTemp = iTemp + 1; 
 
           } 
 
 
 
           strUpper = strUpper.Replace("零拾", "零"); 
 
           strUpper = strUpper.Replace("零佰", "零"); 
 
           strUpper = strUpper.Replace("零仟", "零"); 
 
           strUpper = strUpper.Replace("零零零", "零"); 
 
           strUpper = strUpper.Replace("零零", "零"); 
 
           strUpper = strUpper.Replace("零角零分", "整"); 
 
           strUpper = strUpper.Replace("零分", "整"); 
 
           strUpper = strUpper.Replace("零角", "零"); 
 
           strUpper = strUpper.Replace("零亿零万零圆", "亿圆"); 
 
           strUpper = strUpper.Replace("亿零万零圆", "亿圆"); 
 
           strUpper = strUpper.Replace("零亿零万", "亿"); 
 
           strUpper = strUpper.Replace("零万零圆", "万圆"); 
 
           strUpper = strUpper.Replace("零亿", "亿"); 
 
           strUpper = strUpper.Replace("零万", "万"); 
 
           strUpper = strUpper.Replace("零圆", "圆"); 
 
           strUpper = strUpper.Replace("零零", "零"); 
 
 
 
           // 对壹圆以下的金额的处理 
 
           if (strUpper.Substring(0, 1) == "圆") 
           { 
 
               strUpper = strUpper.Substring(1, strUpper.Length - 1); 
 
           } 
 
           if (strUpper.Substring(0, 1) == "零") 
           { 
 
               strUpper = strUpper.Substring(1, strUpper.Length - 1); 
 
           } 
 
           if (strUpper.Substring(0, 1) == "角") 
           { 
 
               strUpper = strUpper.Substring(1, strUpper.Length - 1); 
 
           } 
 
           if (strUpper.Substring(0, 1) == "分") 
           { 
 
               strUpper = strUpper.Substring(1, strUpper.Length - 1); 
 
           } 
 
           if (strUpper.Substring(0, 1) == "整") 
           { 
 
               strUpper = "零圆整"; 
 
           } 
 
           functionReturnValue = strUpper; 
 
 
 
           if (IsNegative == true) 
           { 
 
               return "负" + functionReturnValue; 
 
           } 
 
           else 
           { 
 
               return functionReturnValue; 
 
           } 
 
       } 

使用图表chart

chartType.Series.Clear();

           Series Series1 = new Series(); 
           chartType.Series.Add(Series1); 
           chartType.Series["Series1"].ChartType = SeriesChartType.Pie;  //图ª?标À¨º类¤¨¤型¨ª饼Ày状Á¡ä图ª? 
           chartType.Series["Series1"].ChartType = SeriesChartType.StackedColumn;  //透ª?明¡Â柱¨´状Á¡ä图ª? 
           //chartType.Series[0].CustomProperties = "DrawingStyle = Cylinder";  //圆2柱¨´形? 
           chartType.Series[0].Palette = ChartColorPalette.SemiTransparent; //颜?色¦? 
           //chartType.Series[0].Palette = ChartColorPalette.BrightPastel;  
           //========标À¨º题¬a================= 
           chartType.Titles.Add("客¨ª户¡ì数ºy据Y分¤?析?"); 
           chartType.Titles[0].ForeColor = System.Drawing.Color.FromArgb(154, 123, 123); 
           chartType.Titles[0].Font = new Font("微¡é软¨¨ª雅?黑¨²", 16f, FontStyle.Regular); 
           chartType.Titles[0].Alignment = ContentAlignment.TopCenter; 
           //chartType.BackColor = Color.Transparent; 
           chartType.ChartAreas[0].BackColor = Color.Transparent; 
           chartType.ChartAreas[0].BorderColor = Color.Transparent; 
            
           chartType.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;//开a启?三¨y维?模¡ê式º?;PointDepth:厚?度¨¨BorderWidth:边À?框¨°宽¨ª 
           chartType.ChartAreas["ChartArea1"].Area3DStyle.Rotation = 15;//起e始º?角?度¨¨ 
           chartType.ChartAreas["ChartArea1"].Area3DStyle.Inclination = 30;//倾?斜¡À度¨¨(0~?90) 
           chartType.ChartAreas["ChartArea1"].Area3DStyle.LightStyle = LightStyle.Realistic;//表À¨ª面?光a泽¨®度¨¨ 
           //chartType.ChartAreas["ChartArea1"].AxisX.Interval = 1; //决?定¡§x轴¨¢显?示º?文?本À?的Ì?间?隔?,ê?1为a强?制?每?个?柱¨´状Á¡ä体¬?都?显?示º?,ê?3则¨°间?隔?3个?显?示º? 
           chartType.ChartAreas["ChartArea1"].AxisX.LabelStyle.Font =new Font("宋?体¬?", 9, FontStyle.Regular); 
           //chartType.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false; 
 
           chartType.Series[0].Points.AddXY(1, 123); 
           chartType.Series[0].Points[0].Label = "diao"; 
           chartType.Series[0].Points[0].Font = new Font("微¡é软¨¨ª雅?黑¨²", 16f, FontStyle.Bold); 
           chartType.Series[0].Points[0].LabelForeColor = Color.Black;// System.Drawing.Color.FromArgb(89, 89, 89); 
            
           chartType.Series[0].Points.AddXY(2, 355); 
           chartType.Series[0].Points[1].Label = "刘¢?备À?"; 
           chartType.Series[0].Points[1].Font = new Font("微¡é软¨¨ª雅?黑¨²", 16f, FontStyle.Bold); 
           chartType.Series[0].Points[1].LabelForeColor = Color.Black; // System.Drawing.Color.FromArgb(189, 89, 89); 
           chartType.Series[0].Points.AddXY(3, 789); 
           chartType.Series[0].Points[2].Label = "张?飞¤¨¦"; 
           chartType.Series[0].Points[2].Font = new Font("微¡é软¨¨ª雅?黑¨²", 16f, FontStyle.Bold); 
           chartType.Series[0].Points[2].LabelForeColor = Color.Black; //System.Drawing.Color.FromArgb(189, 89, 189); 
 
           chartType.Series[0].Points.AddXY(4, 564); 
           chartType.Series[0].Points[3].Label = "关?羽®e38"; 
           chartType.Series[0].Points[3].Font = new Font("微¡é软¨¨ª雅?黑¨²", 16f, FontStyle.Bold); 
           chartType.Series[0].Points[3].LabelForeColor = Color.Black; //System.Drawing.Color.FromArgb(123, 89, 189); 
           chartType.Series[0].Color = Color.Lime; 
           Legend legend1 = new Legend("#VALX"); //饼Ày状Á¡ä图ª?legend 
 
           legend1.Title = "客¨ª户¡ì统ª3计?"; 
           legend1.ForeColor = Color.Black; 
 
 
           //chartType.Series[0].LegendText = chartType.Series[0]. 
           chartType.Legends.Add(legend1); 
           /* 
           LegendCellColumn avgColumn = new LegendCellColumn(); 
           avgColumn.Text = "#AVG{N2}"; 
           avgColumn.HeaderText = "Avg"; 
           avgColumn.Name = "AvgColumn"; 
           avgColumn.HeaderBackColor = Color.WhiteSmoke; 
           chartType.Legends[0].CellColumns.Add(avgColumn); 
           LegendCellColumn avgColumn1 = new LegendCellColumn(); 
           avgColumn1.Text = "#AVG{N2}"; 
           avgColumn1.HeaderText = "Avg1"; 
           avgColumn1.Name = "AvgColumn1"; 
           avgColumn1.HeaderBackColor = Color.WhiteSmoke; 
           chartType.Legends[0].CellColumns.Add(avgColumn1); 
          */ 
 
 
            
 
 
           //X坐Á?标À¨º轴¨¢颜?色¦? 
           chartType.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#cccccc"); 
           chartType.ChartAreas[0].AxisX.LabelStyle.ForeColor = ColorTranslator.FromHtml("#999999");  
           //chartType.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微¡é软¨¨ª雅?黑¨²", 10f, FontStyle.Regular); 
           //X坐Á?标À¨º轴¨¢标À¨º题¬a 
           //chartType.ChartAreas[0].AxisX.Title = "数ºy量¢?(宗Á¨²)"; 
           //chartType.ChartAreas[0].AxisX.TitleFont = new Font("微¡é软¨¨ª雅?黑¨²", 10f, FontStyle.Regular); 
           //chartType.ChartAreas[0].AxisX.TitleForeColor = Color.Black; 
           //chartType.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal; 
           //chartType.ChartAreas[0].AxisX.ToolTip = "数ºy量¢?(宗Á¨²)"; 
           //X轴¨¢网ª?络?线?条¬? 
           //chartType.ChartAreas[0].AxisX.MajorGrid.Enabled = true; 
           chartType.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#cccccc"); 
 
           //Y坐Á?标À¨º轴¨¢颜?色¦? 
           chartType.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#cccccc"); 
           chartType.ChartAreas[0].AxisY.LabelStyle.ForeColor = ColorTranslator.FromHtml("#999999");  
           chartType.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微¡é软¨¨ª雅?黑¨²", 10f, FontStyle.Regular); 
           //Y坐Á?标À¨º轴¨¢标À¨º题¬a 
           //chartType.ChartAreas[0].AxisY.Title = "客¨ª户¡ì数ºy量¢?"; 
           //chartType.ChartAreas[0].AxisY.TitleFont = new Font("微¡é软¨¨ª雅?黑¨²", 10f, FontStyle.Regular); 
           //chartType.ChartAreas[0].AxisY.TitleForeColor = Color.Black; 
           //chartType.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270; 
           //chartType.ChartAreas[0].AxisY.ToolTip = "客¨ª户¡ì数ºy量¢?"; 
           //Y轴¨¢网ª?格?线?条¬? 
           chartType.ChartAreas[0].AxisY.MajorGrid.Enabled = true; 
           chartType.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#cccccc"); 
 
           chartType.ChartAreas[0].AxisY2.LineColor = Color.Transparent; 
           chartType.ChartAreas[0].BackGradientStyle = GradientStyle.TopBottom; 
 

访问mysql数据

 using MySql.Data.MySqlClient;
   String mysqlStr = "Database=数据库;Data Source=ip;User Id=root;Password=密码;pooling=false;CharSet=utf8;port=3306"; 
       
           MySqlConnection mysql = new MySqlConnection(mysqlStr); 
           String sqlSearch = "select host,user from user"; 
           MySqlCommand mySqlCommand = new MySqlCommand(sqlSearch, mysql); 
           mysql.Open(); 
           MySqlDataReader reader = mySqlCommand.ExecuteReader(); 
           try 
           { 
               while (reader.Read()) 
               { 
                   if (reader.HasRows) 
                   { 
                       Console.WriteLine( reader.GetString(1) + "|---" + reader.GetString(0)); 
                   } 
               } 
           } 
           catch (Exception) 
           { 
               Console.WriteLine("查询失败了!"); 
           } 
           finally 
           { 
               reader.Close(); 
           } 
           mysql.Close(); 
 
           /* 
            try 
          { 
            mySqlCommand.ExecuteNonQuery(); 
           } 
           catch (Exception ex) 
           { 
             String message = ex.Message; 
             Console.WriteLine("插入数据失败了!" + message); 
           } 
                    */

linux

cd path_to_your MySql.Data.dll assembly
gacutil -i MySql.Data.dll

在monpdevelop解决方案里右键编辑引用,在.net程序集中点击浏览,选择MySql.Data.dll文件,调用方法同上

mono官方文档

https://www.mono-project.com/docs/database-access/providers/mysql/

sunnyUI

https://gitee.com/yhuse/SunnyUI

c#开发工具MONO

官网 https://www.mono-project.com/

sudo apt-get install monodevelop

asp web service

sudo apt-get install mono-xsp4

Mono下C#调用linux命令

using System.Diagnostics;
Process p = new Process();
       p.StartInfo.FileName = "sh";
       p.StartInfo.UseShellExecute = false;
       p.StartInfo.RedirectStandardInput = true;
       p.StartInfo.RedirectStandardOutput = true;
       p.StartInfo.RedirectStandardError = true;
       p.StartInfo.CreateNoWindow = true;
       p.Start();
       p.StandardInput.WriteLine("ls -l");
       p.StandardInput.WriteLine("exit");
       string strResult = p.StandardOutput.ReadToEnd();
       label1.Text = strResult;
       p.Close();

上传文件

public string SendFile(string fileName, Uri uri, string encodingType = "UTF-8")
       {
           WebClient myWebClient = new WebClient();
           byte[] responseArray = myWebClient.UploadFile(uri, "POST", fileName);
           return Encoding.GetEncoding(encodingType).GetString(responseArray);
       }

需要php服务支持

  SendFile(@"c:\smypOut.jpeg", new Uri("http://****/uploadFile.php"));

启动exe

using System.Diagnostics;
Process.Start("smyp.jsx");

写入文件

       private void writeFile()
       {
           FileStream fs = new FileStream("smyp.txt",FileMode.Create);
           StreamWriter ws = new StreamWriter(fs);
           ws.Write("asda\r\n");
           ws.Write("22222");
           ws.Flush();
           ws.Close();
           fs.Close();
       }

获取本机ip地址

           IPAddress[] iplist =  Dns.GetHostByName(Dns.GetHostName()).AddressList;
           comboBox2.Items.Clear();
           for (int i = 0; i < iplist.Count(); i++)
           {
               comboBox2.Items.Add(iplist[i]);
           }
           if (comboBox2.Items.Count > 0)
               comboBox2.SelectedIndex = 0;