有時候,一些應用程式點一下說明的Link會連線到官方網頁.
在C#很簡單...
System.Diagnostics.Process.Start("IEXPLORE.exe", @"http://www.google.com");
只要加上這一行,開啟IE送你去Google一下...哈哈
歡迎跟我連絡
免安裝程式...哈哈 歡迎聊天
2008年7月22日 星期二
程式中開啟其他應用程式 (C#)
偵測電腦是否連上網際網路
先建立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);
}
}
2008年7月19日 星期六
如何將「Google動態地圖」嵌入到部落格或網頁中?
工具網站: Embed Google 'My Maps'
參考文章:如何將「Google動態地圖」嵌入到部落格或網頁中? By重灌狂人
蠻實用的一個工具網站,隨便點一點,透過Google Maps API產生嵌入程式碼,然後貼到你得BLOG,就OK了
這是我住的地方啦...
嚴重警告:請勿將此座標輸入廵弋飛彈控制電腦中...-_-"
Class與Struct的區別 (C#)
透過下面的例子說明,
using System;
namespace StructVSClass
{
//定義類別
class myRef {public int x;}
//定義結構
struct myVal {public int x;}
class Class1
{
[STAThread]
static void Main(string[] args)
{
myRef r1=new myRef();
myVal v1=new myVal();
r1.x=10;
v1.x=10;
Console.WriteLine("Before :");
Console.WriteLine("r1.x="+r1.x);
Console.WriteLine("v1.x="+v1.x);
myRef r2=r1;
myVal v2=v1;
r1.x=20;
v2.x=30;
Console.WriteLine("After:");
Console.WriteLine("r1.x="+r1.x+";r2.x="+r2.x);
Console.WriteLine("v1.x="+v1.x+";v2.x="+v2.x);
}
}
}
輸出結果:
Before:
r1.x=10
v1.x=10
After:
r1.x=20;r2.x=20
v1.x=10;v2.x=30
所以修改r2會影響r1 (Class是Reference Type)
但是修改v2並不會影響v1 (Struct是Value Type)
2008年7月18日 星期五
資訊技術
If you are a developer, and you don’t like change, you should get out of this business as early as possible.
By David Chappell, Boston, Tech.Ed, 2006
今天在Google收尋MS .Net Framework 3.0相關資料時,看到這句話...
程式設計這一行,我想想成為頂尖的人員,就必須不停的吸收最新的資訊,或許或者說當然,你也可以停至於目前的狀態,但是時代的洪流我想不出2年,就將重你身上流過,而你永遠追趕不上了...
這又是這行的一個哀愁...
當我追逐.Net Framework 3.0的當下,VS2008 with .Net Framework 3.5已然步出Lab...
2008年7月17日 星期四
2008年7月16日 星期三
PMD
2008年7月14日 星期一
序列化小技巧
我相信序的話的過程,一定有一個地方讓你不是很滿意
就是
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema
就是Root Element後的這串字...實在很難看
但是是有辦法解決的
如下:
XmlSerializer s = new XmlSerializer(typeof(APIQCRRD.APIQCRRD_I.transaction));
TextWriter w = new StreamWriter(物件序列化與TX.Properties.Settings.Default.INPUT_LOG);
XmlSerializerNamespaces xmlNameSpace = new XmlSerializerNamespaces();
xmlNameSpace.Add("","");
s.Serialize(w, input, xmlNameSpace);
w.Close();
加上粗體字的程式碼,就OK了...


