using System.Net.Mail;
using System.Net;
class Email
{
public string From {get;set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string Account { get; set; }
public string Password { get; set; }
public string Server { get; set; }
public string Port { get; set; }
public void Send_email()
{
//這邊先設定From,To,Subject,Body,Account,Password,Server,Port
SmtpClient theSmtpClient = new SmtpClient(Server);
NetworkCredential networkcredential = new NetworkCredential(Account, Password);//帳密
theSmtpClient.UseDefaultCredentials = false;
theSmtpClient.Credentials = networkcredential;
int Int_port = Convert.ToInt32(Port);
if (Int_port == 587)
{
theSmtpClient.EnableSsl = true;
}
else//不是587 就是 25
{
theSmtpClient.EnableSsl = false;
}
theSmtpClient.Port = Int_port;
try
{
theSmtpClient.Send(From, To, Subject, Body);
}
catch (Exception)
{
}
}
}
十分簡單,不過要注意的地方就是Port在有SSL加密認證的環境下要用587(ex:gmail),沒有的話則用25即可,然後Using要加入net / net.mail
End
2014年2月14日 星期五
2014年2月13日 星期四
[C#/winform] 呼叫外部程式(傳入參數) + 不會顯示出來 + 結束強制關閉程式
本篇分成三部份來講
a)呼叫外部程式
//呼叫程式(啟動)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
namespace Activate_exe
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main()
{
string target = 目標路徑;
ProcessStartInfo pInfo = new ProcessStartInfo(target);
pInfo.Arguments = "我是參數";
using (Process p = new Process())
{
p.StartInfo = pInfo;
p.Start();
}
}
}
}
//主程式(被呼叫)
//在Program.cs內部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace (被呼叫的程式,名稱自行更改)
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
Application.Run(new Form1());
}
else
{
Application.Run(new Form1(args[0].ToString()));
}
}
}
}
呼叫程式十分簡單,也不需要用到winform,就直接寫在Program.cs即可
一開始記得要加入"using System.Diagnostics;"不然ProcessStartInfo 沒法用,
new一個ProcessStartInfo(要呼叫的程式路徑),然後 ProcessStartInfo.Argument內就是要傳的參數。
(參數遇到空格 疑似可以用\" (http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/88415.aspx)來解決)
傳多個參數,就是使用空格作為分隔 參數A空格B,在Form1値得注意的地方就是有兩個多載,當
沒有參數傳來的時後就用Form1(),有則Form1(args[])
-----------------------------------------------------------------------------------------------------------
被呼叫的程式要接參數,所以main(string[] args
主程式//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Argument_IN_exe
{
public partial class Form1 : Form
{
private string _args;
public Form1()
{
InitializeComponent();
}
public Form1(string value)
{
InitializeComponent();
if (!string.IsNullOrEmpty(value))
{
_args = value;
MessageBox.Show(_args);
}
}
}
}
b)不會顯示出來
要讓程式一開啟就不顯示出來,目前只知道這個方法(最小化+不顯是在系統列)
寫在Form load事件當中,一讀就縮最小,但是有個問題就是你無法停止它(所以才需要
c.強制關閉程式)
private void Form1_Load_1(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.Hide();
}
private void Form1_Shown(object sender, EventArgs e)
{
this.Hide();
}
c)結束強制關閉程式
光有Close()是不夠的,可以試試看Ctrl+Alt+Del,會發現沒有全部關完。
加入Enviroment這行吧!
this.Close();
Environment.Exit(Environment.ExitCode);
參考: http://www.dotblogs.com.tw/atowngit/archive/2009/12/26/12681.aspx
a)呼叫外部程式
//呼叫程式(啟動)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
namespace Activate_exe
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main()
{
string target = 目標路徑;
ProcessStartInfo pInfo = new ProcessStartInfo(target);
pInfo.Arguments = "我是參數";
using (Process p = new Process())
{
p.StartInfo = pInfo;
p.Start();
}
}
}
}
//主程式(被呼叫)
//在Program.cs內部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace (被呼叫的程式,名稱自行更改)
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
Application.Run(new Form1());
}
else
{
Application.Run(new Form1(args[0].ToString()));
}
}
}
}
呼叫程式十分簡單,也不需要用到winform,就直接寫在Program.cs即可
一開始記得要加入"using System.Diagnostics;"不然ProcessStartInfo 沒法用,
new一個ProcessStartInfo(要呼叫的程式路徑),然後 ProcessStartInfo.Argument內就是要傳的參數。
(參數遇到空格 疑似可以用\" (http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/88415.aspx)來解決)
傳多個參數,就是使用空格作為分隔 參數A空格B,在Form1値得注意的地方就是有兩個多載,當
沒有參數傳來的時後就用Form1(),有則Form1(args[])
-----------------------------------------------------------------------------------------------------------
被呼叫的程式要接參數,所以main(string[] args
主程式//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Argument_IN_exe
{
public partial class Form1 : Form
{
private string _args;
public Form1()
{
InitializeComponent();
}
public Form1(string value)
{
InitializeComponent();
if (!string.IsNullOrEmpty(value))
{
_args = value;
MessageBox.Show(_args);
}
}
}
}
b)不會顯示出來
要讓程式一開啟就不顯示出來,目前只知道這個方法(最小化+不顯是在系統列)
寫在Form load事件當中,一讀就縮最小,但是有個問題就是你無法停止它(所以才需要
c.強制關閉程式)
private void Form1_Load_1(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.Hide();
}
private void Form1_Shown(object sender, EventArgs e)
{
this.Hide();
}
c)結束強制關閉程式
光有Close()是不夠的,可以試試看Ctrl+Alt+Del,會發現沒有全部關完。
加入Enviroment這行吧!
this.Close();
Environment.Exit(Environment.ExitCode);
參考: http://www.dotblogs.com.tw/atowngit/archive/2009/12/26/12681.aspx
2014年2月12日 星期三
[C#/winform] Notifyicon(系統列圖示) + ContextMenuStrip(右鍵選單) 設定
首先,先到工具列拉出NotifyIcon 與 ContextMenuStrip兩個工具
NotifyIcon是系統列工具,可以改變圖示(如果覺得它很醜)
ContextMenuStrip則是應用在圖示上面按滑鼠右鍵會出現的選單
如果一開始就不要讓程式show出來,請寫入
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
這樣起始畫面就會縮到最小。(這段要寫再Form Load事件裡面!!)
在ContextMenuStrip當中新增一個選項ex: Exit ,接著點它兩下
進入程式部分:(這邊看要怎樣打都可以)
this.Close();//exit代表關掉程式
最後,在Notify icon的工具欄當中把ContextMenuStrip那一欄改為你所拉進來的ContextMenuStrip的
名稱,這樣它會直接去抓那個工具,很簡單,但是很實用喔!
End
NotifyIcon是系統列工具,可以改變圖示(如果覺得它很醜)
ContextMenuStrip則是應用在圖示上面按滑鼠右鍵會出現的選單
如果一開始就不要讓程式show出來,請寫入
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
這樣起始畫面就會縮到最小。(這段要寫再Form Load事件裡面!!)
在ContextMenuStrip當中新增一個選項ex: Exit ,接著點它兩下
進入程式部分:(這邊看要怎樣打都可以)
this.Close();//exit代表關掉程式
最後,在Notify icon的工具欄當中把ContextMenuStrip那一欄改為你所拉進來的ContextMenuStrip的
名稱,這樣它會直接去抓那個工具,很簡單,但是很實用喔!
End
2014年2月11日 星期二
[C#/Serialize] 簡易版本-Serailize/Deserialize
上一篇序列化/反序列化比較難,這次寫一點簡單的。
首先一樣先建一個類別庫(新增專案->類別庫)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solar_stucture
{
[Serializable]
public class Solar_read
{
public byte Adress = 0;
public byte Function_code = 0;
public byte Byte_count = 0;
}
}
然後建製(F9) 會產生錯誤,很正常。但不是錯誤。(在Debug資料夾內會有.dll檔)
接著,開個新的專案。然後 專案->加入參考...(瀏覽->選剛剛產生的dll檔)
在主程式(或任何你想要加的地方)加入
Solar_read Read_protocol = new Solar_read();
然後使用Read_protocol.Adress,你會發現可以使用!!
沒錯,就把東西存進去(型別要對)
都加完了以後,開始寫入檔案。
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("Read Protocol.dat", FileMode.Create, FileAccess.Write, FileShare.Read);
formatter.Serialize(stream, Read_protocol);
stream.Close();
記得dll檔那邊要有[Serializable]不然這邊執行上會出現serialize的異常。序列化到此結束!
基本的反序列化也十分簡單:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Solar_stucture;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SolarControl_Front
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(序列化存的檔案路徑, FileMode.Open, FileAccess.Read, FileShare.Read);
Solar_read read = (Solar_read)formatter.Deserialize(stream);
MessageBox.Show(read.Adress.ToString());
stream.Close();
}
}
}
首先一樣先建一個類別庫(新增專案->類別庫)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solar_stucture
{
[Serializable]
public class Solar_read
{
public byte Adress = 0;
public byte Function_code = 0;
public byte Byte_count = 0;
}
}
然後建製(F9) 會產生錯誤,很正常。但不是錯誤。(在Debug資料夾內會有.dll檔)
接著,開個新的專案。然後 專案->加入參考...(瀏覽->選剛剛產生的dll檔)
在主程式(或任何你想要加的地方)加入
Solar_read Read_protocol = new Solar_read();
然後使用Read_protocol.Adress,你會發現可以使用!!
沒錯,就把東西存進去(型別要對)
都加完了以後,開始寫入檔案。
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("Read Protocol.dat", FileMode.Create, FileAccess.Write, FileShare.Read);
formatter.Serialize(stream, Read_protocol);
stream.Close();
記得dll檔那邊要有[Serializable]不然這邊執行上會出現serialize的異常。序列化到此結束!
基本的反序列化也十分簡單:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Solar_stucture;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SolarControl_Front
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(序列化存的檔案路徑, FileMode.Open, FileAccess.Read, FileShare.Read);
Solar_read read = (Solar_read)formatter.Deserialize(stream);
MessageBox.Show(read.Adress.ToString());
stream.Close();
}
}
}
一樣的東西,只是變成Deserialize(Stream stream),接的格式是用你所創建的Class格式喔!
MessageBox只是確認有弄對而已。 Over
MessageBox只是確認有弄對而已。 Over
[C#/進制轉換] byte資料 轉換 ASCII碼
先看看程式碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ByteToASCII
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] toASCII = new byte[4];
toASCII[0] = Convert.ToByte(textBox1.Text,10);
toASCII[1] = Convert.ToByte(textBox2.Text,10);
toASCII[2] = Convert.ToByte(textBox3.Text,10);
toASCII[3] = Convert.ToByte(textBox4.Text,10);
textBox5.Text = System.Text.Encoding.ASCII.GetString(toASCII);
}
}
}
先new一個byte陣列,利用Convert.ToByte(String,多少進位)來轉換,存如byte陣列當中
之後用Sytem.Text.Encoding.ASCII.GetString(byte陣列) 即可轉換
End
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ByteToASCII
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] toASCII = new byte[4];
toASCII[0] = Convert.ToByte(textBox1.Text,10);
toASCII[1] = Convert.ToByte(textBox2.Text,10);
toASCII[2] = Convert.ToByte(textBox3.Text,10);
toASCII[3] = Convert.ToByte(textBox4.Text,10);
textBox5.Text = System.Text.Encoding.ASCII.GetString(toASCII);
}
}
}
先new一個byte陣列,利用Convert.ToByte(String,多少進位)來轉換,存如byte陣列當中
之後用Sytem.Text.Encoding.ASCII.GetString(byte陣列) 即可轉換
End
訂閱:
文章 (Atom)