歡迎跟我連絡

本頁最下方有Web MSN可以直接跟我交談喔!
免安裝程式...哈哈 歡迎聊天

2008年8月5日 星期二

Ping in C#

有時候需要知道另一部電腦是否還"活著",可以這樣做...
private void btn_Click(object sender, EventArgs e)
{
try
{
Ping ping = new Ping();
PingReply pingreply = ping.Send("xxx.xxx.xxx.xxx");//IP Address
MessageBox.Show(pingreply.Status.ToString());
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
記得要using System.Net.NetworkInformation 這個NameSpace

2008年7月31日 星期四

生命中不能承受之輕

這句話常聽到或看到,大概的意義是說,"生命中有很多事,看似輕如鴻毛,卻讓人難以承受."

記得這是多年前米蘭.昆德拉在"生命中不能承受之輕"一書,揭示的這個問題,但是它也是凡人跟我們一樣,無力去解決著個問題.
生命有著"一次"的特性,過了就無法重新來過,沒有彩排沒有預演更沒有草稿,任何的選擇有就變的毫無意義.因為每次的選擇,除了媚俗我們還有其他選擇嗎?怎知,在抵抗媚俗的另一個選擇,卻是更加的媚俗,選也不是,不選也不是,進退維谷...
生命中有很多的不能承受之重,像是工作,生活,親情,愛情,人際關係,因人而異.因為這些的重,往往會忽略了那些生命之中的輕,而這些輕有時候是更讓人無法承受之...

2008年7月30日 星期三

老闆~我要剪成跟照片的一模一樣




南拳媽媽 下雨天 <轉載>



南拳媽媽 - 下雨天
作詞:梁心頤(Lara) / 作曲:張傑
下雨天了 怎麼辦 我好想你
我不敢打給你 我找不到原因
為什麼失眠的聲音 變得好熟悉
沈默的場景 做你的代替 陪我等雨停

期待讓人越來越沉溺(疲憊)
誰和我一樣 等不到他的誰
愛上你我總在學會 寂寞的滋味
一個人撐傘 一個人擦淚 一個人好累

怎樣的雨 怎樣的夜 怎樣的我 能讓你更想念
雨要多大 天要多黑 才能夠有你的體貼
其實沒有我你分不出哪些差別
結局那還能多明顯
別說你會難過 別說你想改變
被愛的人不用道歉



雨季說:
愛上那句"其實沒有我你分不出哪些差別"...
好好聽...

2008年7月27日 星期日

Using the VB.net My namespace in C#

首先Add a New Reference


然後Add a using clause
using Microsoft.VisualBasic.Devices;

最後程式中引用
Computer myComputer = new Computer();
myComputer.Audio.Play(@"c:\WINDOWS\Media\chimes.wav");

該NameSpace提供了下列類別:
Audio
Clipboard
Clock
FileSystem
Info
Keyboard
Mouse
Name
Network
Ports
Registry
Screen

VB.NET替Programmer提供的很方便的類別,透過上述的方式也方便C#的使用者快速建立程式

2008年7月23日 星期三

String 2 Stream (C#)

有時候會用到的東西...
string myString = "abcdefg";
Stream myStream = new MemoryStream(Encoding.Default.GetBytes(myString));
就這樣而已...

2008年7月22日 星期二

程式中開啟其他應用程式 (C#)

有時候,一些應用程式點一下說明的Link會連線到官方網頁.
在C#很簡單...
System.Diagnostics.Process.Start("IEXPLORE.exe", @"http://www.google.com");

只要加上這一行,開啟IE送你去Google一下...哈哈

偵測電腦是否連上網際網路

先建立InternetStatus 物件
using System;
using System.Net;

class InternetStatus
{
public static bool IsConnectedToInternet()
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://www.google.com");
// Perform GET
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (HttpStatusCode.OK == response.StatusCode)
{
// HTTP = 200, close the request and return true
response.Close();
return (true);
}
else
{
// Other status; return false
return(false);;
}
}
catch (Exception e)
{
return (false);
}
}
}

使用方式:
private void button_Click(object sender, EventArgs e)
{
if (InternetStatus.IsConnectedToInternet())
label6.Text = "Status:Online";
else
label6.Text = "Status:Offline";
}



雨季說:
這個方式是透過HttpWebRequest對Google網站發出Request並透過HttpWebResponse取得網站回應,有回應就代表連上網際網路了...
不過最好加一個Thread處理這一段程式,避免UI短暫Hung住...

自動撥接ADLS (C#)

先建立Dailer物件

using System;
using System.Windows.Forms;

public class Dialer{
[System.Runtime.InteropServices.DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern bool InternetGetConnectedState(ref Int32 lpdwFlags, Int32 dwReserved);

[System.Runtime.InteropServices.DllImport("Wininet.dll", EntryPoint = "InternetDial", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern Int32 InternetDial(IntPtr hwndParent, string lpszConnectoid, Int32 dwFlags, ref Int32 lpdwConnection, Int32 dwReserved);

[System.Runtime.InteropServices.DllImport("Wininet.dll", EntryPoint = "InternetHangUp", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern Int32 InternetHangUp(Int32 lpdwConnection, Int32 dwReserved);

private enum Flags : int{
//Local system uses a LAN to connect to the Internet.
INTERNET_CONNECTION_LAN = 0X2,
//Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM = 0X1,
//Local system uses a proxy server to connect to the Internet.
INTERNET_CONNECTION_PROXY = 0X4,
//Type Visual Basic 6 code here...
//Local system has RAS installed.
INTERNET_RAS_INSTALLED = 0X10
}

//Declaration Used For InternetDialUp.
private enum DialUpOptions : int{
INTERNET_DIAL_UNATTENDED = 0X8000,
INTERNET_DIAL_SHOW_OFFLINE = 0X4000,
INTERNET_DIAL_FORCE_PROMPT = 0X2000,
INTERNET_AUTODIAL_FORCE_UNATTENDED = 0x0002,
INTERNET_AUTODIAL_FORCE_ONLINE = 0x0001
}

private const int ERROR_SUCCESS = 0X0;
private const int ERROR_INVALID_PARAMETER = 0X87;
private Int32 mlConnection;
private string _撥接名稱;
public Dialer(){}
public string 撥接名稱
{
set { _撥接名稱 = value; }
get { return _撥接名稱; }
}

public string GetConnectionType(){
Int32 lngFlags = 0;
if (InternetGetConnectedState(ref lngFlags, 0)){
//connected.
if ((lngFlags & (int)Flags.INTERNET_CONNECTION_LAN) != 0){
//LAN connection.
return "LAN connection.";
}else if ((lngFlags & (int)Flags.INTERNET_CONNECTION_MODEM) != 0){
//Modem connection.
return "Modem connection.";
}else if ((lngFlags & (int)Flags.INTERNET_CONNECTION_PROXY) != 0){
//Proxy connection.
return "Proxy connection.";
}
return "Not connected.";
}else{
//not connected.
return "Not connected.";
}
}
public void Connect(){
Int32 DResult = 0;
DResult = InternetDial(IntPtr.Zero, _撥接名稱, Convert.ToInt32(DialUpOptions.INTERNET_AUTODIAL_FORCE_ONLINE), ref mlConnection, 0);

if (DResult == ERROR_SUCCESS)
MessageBox.Show("Dial Up Successful");
else
MessageBox.Show("UnSuccessFull,Error Code:"+DResult.ToString());
}
public void Disconnect(){
Int32 Result = 0;
if (!(mlConnection == 0)){
Result = InternetHangUp(mlConnection, 0);
if (Result == 0)
MessageBox.Show("Hang up successful");
else
MessageBox.Show("Hang up NOT successful");
}
else
MessageBox.Show("You must dial a connection first!");
}
}

使用
private void button_Click(object sender, EventArgs e)
{
if (textBox2.Text == string.Empty)
MessageBox.Show("請填入撥接名稱"+System.Environment.NewLine+"開始工作表->連線到 其中之一的名稱");
else
{
ADSL.撥接名稱 = textBox2.Text;
ADSL.Connect();
}
}


雨季說:
其中撥接名稱,就是你建立的ADSL連線名稱,第一次使用會出現一個對話窗,只要勾選不在顯示,以後就不會在出現了...
因為幾乎大家都是申請浮動IP,所以幾乎都會自動斷線,可以利用上面Dailer物件,寫個自動撥接程式...

透過Google Mail Server郵寄郵件

using System.Net;

private bool MailTo(){
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(
"我的完整帳號","對方的完整帳號","主旨","信件內容");
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com");
mailClient.Port = 587;
mailClient.Credentials = new NetworkCredential("我的完整帳號","我的密碼");
mailClient.EnableSsl = true;
try{
mailClient.Send(message);
return(true);
}
catch (Exception ex){
return(false);
}
}

MSN狀態(我在線上時,可以跟我交談喔)