2012年5月2日 星期三

[C#] Windows FireWall 防火牆操作

防火牆的控制。

     功能

  1. -添加防火牆例外端口
  2. -將應用程序添加到防火牆例外
  3. -刪除防火牆例外端口
  4. -刪除防火牆例外中應用程序

按我下載類別

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);
            }