2012年3月26日 星期一

[C#] IP,Ping

CMD 下的 Ping 工具一直是很簡便的 網路連線偵測工具,本範例是使用

Net.framework 2 的 Ping 類別 來達成目的。

 

專案下載

KM-000

private void button1_Click(object sender, EventArgs e)
        {
            Ping p1 = new Ping();
            p1.PingCompleted += new PingCompletedEventHandler(this.PingCompletedCallBack);
            if (textBox1.Text.Trim().Length == 0)
            {
                return;
            }
            p1.SendAsync(textBox1.Text.Trim(), null);
        }

private void PingCompletedCallBack(object sender, PingCompletedEventArgs e)
        {
            listBox1.Items.Clear();

            if (e.Cancelled)
            {
                listBox1.Items.Add("Ping Canncel");
                return;
            }

            if (e.Error != null)
            {
                listBox1.Items.Add("e.Error.Message");
                return;
            }

            PingReply reply = e.Reply;
            listBox1.Items.Add(reply.Address + " " + reply.Status.ToString());
            if (reply.Status == IPStatus.Success)
            {
                listBox1.Items.Add("RoundTrip time:" + reply.RoundtripTime);
                listBox1.Items.Add("Time to live:"+reply.Options.Ttl);
                listBox1.Items.Add("Don't fragment:"+reply.Options.DontFragment);
                listBox1.Items.Add("Buffer size:"+reply.Buffer.Length);
            }
        }

2012年3月6日 星期二

[C#] MailMessage,SmtpClient,寄信,E-Mail


使用 smtpClient 送信

//引用
using System.Net.Mail;
using System.Net.Mime;

//主要方法
 public void send_email(string msg, string mysubject, string address)
        {
            MailMessage message = new MailMessage(test@123.co.jp, address);//MailMessage(寄信者, 收信者)
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;//E-mail編碼
            message.Subject = mysubject;//E-mail主旨
            message.Priority = MailPriority.Normal; //優先權
            message.Body = msg;//E-mail內容
            string file_name = @"C:\Users\km\Desktop\fdkbc.xls"; //要寄送的添付檔案
            Attachment data = new Attachment(file_name, MediaTypeNames.Application.Octet);
            ContentDisposition disposition = data.ContentDisposition;
            disposition.CreationDate = System.IO.File.GetCreationTime(file_name);
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file_name);
            disposition.ReadDate = System.IO.File.GetLastAccessTime(file_name);
            message.Attachments.Add(data);
            SmtpClient smtpClient = new SmtpClient("172.23.120.254", 25);//設定E-mail Server和port
            smtpClient.Send(message);
        }

//使用方法
 send_email("測試內容", "測試主旨標題", "mailto:kuomingwang@gmail.com%22);//呼叫send_email函式測試

2012年1月18日 星期三

[C#] 防止多開的2個處理方法

方法1:
 //直接砍 Process
  System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName(Application.ProductName);
            if (p.Length > 1)
            {
                for (int i = 0; i < p.Length; i++)
                {
                    p[i].Kill();
                }
            }

方法2:

//宣告
        [DllImport("kernel32.dll", EntryPoint = "CreateMutexA", SetLastError = true)]
        public static extern IntPtr CreateMutex(IntPtr securityAttributes, bool initialOwner, string name);
        [DllImport("kernel32.dll", EntryPoint = "ReleaseMutex", SetLastError = true)]
        public static extern IntPtr ReleaseMutex(IntPtr handle);

        public static bool IsMutexDataExist(string name)
        {
            bool returnValue = false;
            int ERROR_ALREADY_EXISTS = 183;
            IntPtr handle = CreateMutex(IntPtr.Zero, true, name);
            if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS)
            {
                returnValue = true;
            }
            ReleaseMutex(handle);
            return returnValue;
        }

//使用方法
             if (IsMutexDataExist("BetWorldAuthServer") == true)
            {
                //MessageBox.Show("已經在執行中", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Environment.Exit(0);
            }

2011年12月21日 星期三

[C#] 控制 WinForm 顯示於延伸視窗(延伸顯示器)、雙螢幕

有個需求是 必須要指定某個 WinForm 開啟於 延伸視窗



那個該如何讓 程式 開啟的時候 直接顯示於 延伸顯示器呢?
請在 Form1.Designer.cs 內追加 即可。
DesktopLocation 裡面的 X 的值 請務必大於 本機顯示器的 X 解析度.


2011年12月20日 星期二

[C#] 使用 ZedGraph Chart 來製作圖表(開源控件)

由於工作單位裡面老舊電腦比較多(還有一些是 Windows 2000)
所以 Net.Framework 只能使用到 2.0 版本。

需求是 必須產生即時線圖,比較過幾種開源控件,最終採用 ZedGraph

完整專案下載

使用效果











//首先要加入參考 ZedGraph


               GraphPane myPane = null;
                myPane = zg1.GraphPane;
                myPane.CurveList.Clear();
                myPane.GraphObjList.Clear();
                //資料來源 處理
                int[] x = { 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
                int[] y = { 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70, 20, 100, 50, 20, 70};
                //資料填充
                PointPairList list = new PointPairList();
                list.Clear();
                for (int i = 0; i < x.Length; i++)
                {
                    list.Add(x[i], y[i]);
                }
                //圖形建立
                BarItem myCurve = myPane.AddBar("作業 OK品 實積", list, Color.Blue);  //建立長條圖
                //BarItem.CreateBarLabels(myPane, false, "f0"); //每個長條圖 都顯示個別數據
                //介面屬性處理
                myPane.Title.IsVisible = false; //主標題是否 顯示
                //myPane.Title.Text = "Emp"; //主標題
                myPane.Title.FontSpec.FontColor = Color.Green;
                myPane.XAxis.Title.IsVisible = false; //X軸 是否顯示 title
                //myPane.XAxis.Title.Text = "Hr"; //X 軸 Title
                //myPane.XAxis.Title.FontSpec.Size = 40; //X 軸 Title 文字Size
                string[] xx = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" };
                myPane.XAxis.Scale.TextLabels = xx; //X軸刻度資料來源
                myPane.XAxis.Type = AxisType.Text; //X軸刻度資料型態
                myPane.XAxis.Scale.FontSpec.Size = 25;
                //myPane.XAxis.MajorGrid.IsVisible = true; //顯示格點
                //myPane.XAxis.MajorGrid.Color = Color.Pink; //格點顏色
                myPane.YAxis.Title.Text = "Pcs"; //Y 軸的 Title
                myPane.YAxis.Title.FontSpec.Size = 25;
                myPane.YAxis.Scale.FontSpec.Size = 25;
                myPane.YAxis.MajorGrid.IsVisible = true;
                myPane.YAxis.MajorGrid.Color = Color.Black;
                //myPane.YAxis.Scale.MajorStep = 10; //Y軸資料 間隔
                //myPane.YAxis.Scale.Max = 120; //Y軸 最大值
                myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, -45F); //Chart 填色
                myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), -45F); //外部 填色
                myPane.Legend.Position = ZedGraph.LegendPos.InsideTopRight; //圖例放置位置
                myPane.Legend.FontSpec.Size = 20;
                //畫面產生
                zg1.AxisChange();
                //重要! 若資料有變更時 畫面重整
                zg1.RestoreScale(myPane);



#相關網站:
1.)Think專欄  介紹一些常用函數。
2.)oayx 一些使用說明及資源。
3.)Codeproject 開源Project