異世界


顯示具有 基礎 標籤的文章。 顯示所有文章
顯示具有 基礎 標籤的文章。 顯示所有文章

2018年8月18日 星期六

HTTP METHOD的不同性質分析:GET,POST和其他4種METHOD的差別

一篇文章解釋HTTP Method

轉貼網址:http://mikuweb.blogspot.tw/2015/10/http-methodgetpost4method.html

HTTP協定中定義了多種不同的method,瀏覽器或是其他程式再進行HTTP連線時,會使用這些method來進行連線並取得回復。
這些method到底有甚麼差別呢?

這裡列舉常見的六種HTTP Method分別是head,get,post,delete,put,patch。
其實還有很多其他得Method,為甚麼要特別提到這六種呢?因為這六種跟網頁的資料有非常大的關係。

先從最常見的get和post說起吧。
一個剛接觸網頁的人或是早期用PHP寫網頁的人常常混用get和post,因為兩者的功能基本上是相同的,而且以前主流是使用網址的Query String和不同的URL來區分功能(比如上傳和搜尋),但是現在愈來愈多網頁用API導向,也就是一個URL負責一個「業務」,對於上傳和瀏覽分別用不同的Method來處理。

舉例來說,我們現在有一個可以留言的留言板,我們通常會使用get來取得現在的留言,而要新增新的留言時,我們會post到這個位置(有點像是問服務生今天的菜單,然後跟同一個服務生點餐)。一些比較早期的網頁則可能混用get和post,把瀏覽留言和新增留言放在不同網址(有點像把領錢和存錢規劃在不同櫃台辦理)。

所以我們現在知道,不同的Method就是對同一件事情做不同的操作。
再來舉服務生點餐的例子,
假設現在我們要點餐,我們必須先知道菜單是甚麼(get),
我們會向服務生點餐(post),
我們想要取消剛才點的餐點(delete),
我們想要重新點一次(put),
我們想要加點甜點和飲料(patch)。

到這裡我們已經提到了主要的Method了(head是取得get的http header而不取得內容,性質上我們可以當作跟get一樣),至於這幾種Method的行為我們可以統整一下:
head:和get一樣,只是head只會取的HTTP header的資料。
get:取得我們想要的資料。
post:新增一項資料。(如果存在會新增一個新的)
put:新增一項資料,如果存在就覆蓋過去。(還是只有一筆資料)。
patch:附加新的資料在已經存在的資料後面。(資料必須已經存在,patch會擴充這項資料)
delete:刪除資料。

說了這麼多,其實這些Method也只是HTTP建議我們這樣做而已,至於網站架設者是不是有遵守又是令外一回事了。(比如說他可以選擇用delete來新增留言)
不過,遵守規定是有好處的,因為瀏覽器會根據不同的Method做不同的事,Google的機器人也會根據連結的不同做不一樣的事(Google有一隻爬蟲會不停更新全世界的網站)。
舉一個例子,現在有一個行事曆網頁,有很多的行事曆事項,旁邊有修改和刪除的按鈕。
如果這些按鈕使用get,Google的爬蟲就會全部去按一遍(他想要知道你的網站的所有內容,他才能建立索引),然後我們的行事曆就被刪光光了!!
如果我們把刪除按鈕改成用delete,Google就不會去按了(他只會按get的連結)。

另外一個例子是,假設現在網路狀況不好,我們用網路ATM轉帳,傳送出去後卻因為網路不穩而瀏覽器沒收到回應,這時如果瀏覽器自動重新整理,就會送出兩筆轉帳要求!!
但是如果轉帳使用post,瀏覽器不但不會自動重新整理,甚至使用者要求重新整理時還會跳出警告訊息。

上面兩個例子告訴我們,不同的Method間接的告訴使用者應該怎麼樣操作這些動作。
這裡有兩個性質,Safe是是否安全,如果會修改資料就是不安全,間接說明是否可以快取(不重複發出請求);Idempotent則是是否可以在不確定有沒有成功送出時重新發出請求。
我們可以整理成以下表格:

image


我們發現,除了get之外的Method都會修改到資料,所以如果我們要寫一支爬蟲,我們就必須要注意,不可以輕易的去觸動除了get以外的Method,因為我們可能會無意間改到資料。
而get,put,delete都可以重新整理,是因為put會覆蓋掉原本的資料(跟服務生說「我」要吃牛排說10次還是只會來一份牛排),delete則是一定會清除資料(跟服務生說我不吃了),所以這3個Method都可以重複呼叫,所以當網路不穩定時瀏覽器可以被允許自動重新整理。

想想如果我們違反這些規定會有甚麼結果。
我們重複和服務生說「一份牛排」(POST)那結果將是我們點了很多份牛排。
我們重複和服務生說「加一份甜點」(patch)那結果就是很多份甜點。

我們架設網站和寫爬蟲時,如果可以儘量遵守這些協定,就可以避免不必要的麻煩。

2017年10月17日 星期二

UDP Server (更新)

/* ==  如何使用 jUdpServer  ================================================================

1. 將該程式 namespace 改成與主程式相同命名空間

2. 於主程式 :
 2.1. 定義一個 UDP Server 欄位:
     jUdpServer jUDP_Server;

 2.2. 建一個事件處理程序,來處理 UdpReceived 事件
     private void DoUdpReceived(object sender, UdpEventArgs e)
     {
            Byte[] bytes = jUdpServer.StreamToBytes(e.MSG);

            //.. Show UDP MSG  ..//
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(BitConverter.ToString(bytes));
            txtUdpView.Text += sb.ToString();

     }
 2.3. 建一個 Function 來啟動與監聽 UDP 如下:
     private void UDP_StartListen(object sender)
     {
         // NPortUDP = new jUdpServer(sender, txtIP.Text, Convert.ToInt16(txtPort.Text));
         jUDP_Server = new jUdpServer(sender, Convert.ToInt16(txtLocalPort.Text));
         jUDP_Server.UdpReceived += DoUdpReceived; //..指定 UdpReceived 事件處理程序
         jUDP_Server.StartListen();
     }

3. 內已包含 UDP-Send 函式,
   函式結構 : UDP_Send(String RemoteIP, int RemotePort, byte[] TxBuffer, int ByteCount);
   用法如下:
     byte[] bytes = new byte[1,2,3,....,9];
     jUDP_Server.UDP_Send("192.168.100.100", 4001, bytes, 5);
======================================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace MyUdpServer
{
   
    /// <summary>
    /// 事件委派宣告
    /// </summary>
    public delegate void UdpEventHandler(object sender, UdpEventArgs e);
   // public delegate void AlarmSummeryEventHandler(object sender, AlarmSummeryEventArgs e);
   
    public class jUdpServer
    {     
        #region Client端 方法
        Socket m_SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPAddress m_RemoteAddress = new IPAddress(0);
        public int UDP_Send(String RemoteIP, int RemotePort, byte[] TxBuffer, int ByteCount)
        {
            //("遠端IP位置 : ");
            // IPAddress m_RemoteAddress = IPAddress.Parse(RemoteIP) ;
            // if (m_RemoteAddress != null)
            if (IPAddress.TryParse(RemoteIP, out m_RemoteAddress))
            {
                /*初始化IPEndPoint物件,表示要連結的遠端IP位置和Port號*/
                EndPoint m_IPPoint = new IPEndPoint(m_RemoteAddress, RemotePort);
                try
                {
                    /*送出訊息*/
                    m_SocketClient.SendTo(TxBuffer, m_IPPoint);
                    return (0);
                }
                catch
                {
                    return (-1);
                }
            }

            return (-1);           
        }
        #endregion

        #region 公用程式 Bytes、Stream 轉換
        /// <summary>
        /// 將 Byte陣列 轉為 Stream
        /// </summary>
        /// <returns>Stream</returns>
        public static Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }

        public static Stream BytesToStream(byte[] bytes, int Length)
        {
            Stream stream = new MemoryStream(bytes, 0, Length);
            return stream;
        }
        /// <summary>
        /// 將 Stream 轉為 Byte陣列
        /// </summary>
        public static byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            /*將 stream 指針指向開頭*/
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// <summary>
        /// C# byte[]合併
        /// </summary>
        /// <returns>合併後的Byte[]</returns>
        private byte[] CombomBinaryArray(byte[] srcArray1, byte[] srcArray2)
        {
            //根据要合併的两Byte[]數量新建一個Byte[]
            byte[] newArray = new byte[srcArray1.Length + srcArray2.Length];

            Array.Copy(srcArray1, 0, newArray, 0, srcArray1.Length);
            Array.Copy(srcArray2, 0, newArray, srcArray1.Length, srcArray2.Length);
            return newArray;
        }






        #endregion

        /* 建構式 */
        public jUdpServer(object Owner, string IP, int APort)
        {
            mOwner = Owner;
            ServerPort = APort;
            ServerIP = IP;         
        }
        public jUdpServer(object Owner, int APort)
        {
            mOwner = Owner;
            ServerPort = APort;
            ServerIP = "0.0.0.0";
        }
        ~jUdpServer()
        {
            try { mSocketServer.Disconnect(true); }
            catch { }
            try { mSocketServer.Close(); }
            catch { }
        }

        /* 欄位 */
        // private bool mEnabled = false;
        private SetUdpReceived mSetUdpReceivedDlg; // 委派欄位
        private object mOwner; // 儲存擁有者
        private byte[] mRecvBuffer = new byte[10240]; // 接收資料的緩衝區
        private int mRecvBufferSize = 10240; // 接收資料的緩衝區大小
        private EndPoint mRemotePoint = new IPEndPoint(IPAddress.Any, 0); // 初始化遠端IP和 Port端點物件
        private Socket mSocketServer;// = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // 本機端 Socket物件       
       
        /* 屬性 */
        public string ServerIP { get; set; }
        public int ServerPort { get; set; }
        //public bool Enabled
        //{
        //    get { return mEnabled; }
        //    set
        //    {
        //        StartListen();
        //        mEnabled = value;
        //    }
        //}
        public int RecvBufferSize
        {
            get { return mRecvBufferSize; }
            set
            {
                if (mRecvBufferSize != value)
                {
                    mRecvBuffer = new byte[value];
                    mRecvBufferSize = value;
                }
            }
        }

        /* 方法 */       
        private void DoUdpReceivedText(Stream msg, IPEndPoint ipend)// 委派處理函式
        {
            UdpEventArgs e = new UdpEventArgs(msg, ipend);

            if (UdpReceived != null) //. 當事件變數不為 null 則執行之
            {
                UdpReceived(this, e);
            }
        }
        private void EndRecvCallBack(IAsyncResult result)// 非同步接收資料的回呼
        {
            try
            {
                /*傳回非同步物件*/
                Socket mResultSocket = result.AsyncState as Socket;

                /*呼叫 EndReceiveFrom 非同步接收*/
                int mRecvBytes = mResultSocket.EndReceiveFrom(result, ref mRemotePoint);

                /*將資料置入Stream */
                Stream mStream = BytesToStream(mRecvBuffer, mRecvBytes);

                /* Invoke */
                ((System.Windows.Forms.Control)mOwner).Invoke(
                    mSetUdpReceivedDlg,
                    mStream,
                    (IPEndPoint)mRemotePoint
                  ); //.. List<byte>

            }
            catch (Exception ex)
            {
                //((System.Windows.Forms.Control)m_Owner).Invoke(m_SetUdpReceivedDlg, ex.Message); //??????
            }
            finally
            {
                /*重新呼叫非同步接收*/
                mSocketServer.BeginReceiveFrom(
                    mRecvBuffer,
                    0,
                    mRecvBuffer.Length,
                    SocketFlags.None,
                    ref mRemotePoint,
                    new AsyncCallback(EndRecvCallBack),
                    mSocketServer
                  );
            }

        }
        public void StartListen()
        {
            // 開啟伺服器的 "ServerPort" 連接埠,用來接收任何傳送到本機的訊息。
            //IPEndPoint ipep = new IPEndPoint(IPAddress.Any, ServerPort);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ServerIP), ServerPort);

            // 建立接收的 Socket,並使用 Udp 的 Datagram 方式接收。
            mSocketServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            // 綁定 Socket (newsock) 與 IPEndPoint (ipep),讓 Socket 接收 4001 埠的訊息。
            mSocketServer.Bind(ipep);
           
            mSetUdpReceivedDlg = DoUdpReceivedText; /*設定委派處理函式*/

            //..啟動非同步接受資料
            mSocketServer.BeginReceiveFrom(
                mRecvBuffer,
                0,
                mRecvBuffer.Length,
                SocketFlags.None,
                ref mRemotePoint,
                new AsyncCallback(EndRecvCallBack),
                mSocketServer
              );
        }

        /* 簽章 */
        private delegate void SetUdpReceived(Stream msg, IPEndPoint ipend);// 委派宣告
       
        /* 事件 */
        public event UdpEventHandler UdpReceived; // UDP Received 事件       
    }

    /// <summary>
    /// 定義專用事件參數類別
    /// </summary>
    public class UdpEventArgs : EventArgs
    {
        private IPEndPoint mIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
        private Stream mMSG = new MemoryStream();
        public UdpEventArgs(Stream msg, IPEndPoint ipend)
        {
            MSG = msg;
            mIpEndPoint.Address = ipend.Address;
            mIpEndPoint.Port = ipend.Port;
        }
        public IPEndPoint EndPoint
        {
            get { return mIpEndPoint; }
            private set { mIpEndPoint = value; }
        }
        public Stream MSG
        {
            get { return mMSG; }
            private set { mMSG = value; }
        }
    }

   
}

2016年6月22日 星期三

[C#] FTP 下載、 上載和刪除檔案

FTP 下載、 上載和刪除檔案

--------------------------------------------------------------------------------------------------
1.   File Download From FTP:
string localPath = @"G:\FTPTrialLocalPath\";
string fileName = "arahimkhan.txt";

FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDownload.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();

Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create); 

int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length); 

while (bytesRead > 0)
{
 writeStream.Write(buffer, 0, bytesRead);
 bytesRead = responseStream.Read(buffer, 0, Length);
} 

responseStream.Close();
writeStream.Close();

requestFileDownload = null;
responseFileDownload = null;




2. File Upload to FTP:

string localPath = @"G:\FTPTrialLocalPath\";
string fileName = "arahimkhan.txt";

FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/Destination/" +fileName);
requestFTPUploader.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;

FileInfo fileInfo = new FileInfo(localPath + fileName);
FileStream fileStream = fileInfo.OpenRead();

int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];

Stream uploadStream = requestFTPUploader.GetRequestStream();
int contentLength = fileStream.Read(buffer, 0, bufferLength);

while (contentLength != 0)
{
 uploadStream.Write(buffer, 0, contentLength);
 contentLength = fileStream.Read(buffer, 0, bufferLength);
}

uploadStream.Close();
fileStream.Close();

requestFTPUploader = null;
          




3. File Delete From FTP:

string fileName = "arahimkhan.txt";

FtpWebRequest requestFileDelete = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDelete.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;

FtpWebResponse responseFileDelete = (FtpWebResponse)requestFileDelete.GetResponse();
          




4. Retrieve File List from FTP Directory:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source");
request.Credentials = new NetworkCredential("khanrahim", "arkhan22");
request.Method = WebRequestMethods.Ftp.ListDirectory;

StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());

string fileName = streamReader.ReadLine();

while (fileName != null)
{
 Console.Writeline(fileName );
 fileName = streamReader.ReadLine();
}

request = null;
streamReader = null;
          



5. Retrieve File List from Local Directory:

string localPath = @"G:\FTPTrialLocalPath\";

string[] files = Directory.GetFiles(localPath);

foreach (string filepath in files)
{
 string fileName = Path.GetFileName(filepath);
 Console.WriteLine(fileName);
}

2013年4月20日 星期六

處理 ini 之 Class

使用win32 建立一個 ini 檔案管理 Class => SetupIni

   1: public class SetupIni
   2: {
   3:     public string path;
   4:     [DllImport("kernel32", CharSet = CharSet.Unicode)]
   5:     private static extern long WritePrivateProfileString(string section,
   6:         string key, string val, string filePath);
   7:  
   8:     [DllImport("kernel32", CharSet = CharSet.Unicode)]
   9:     private static extern int GetPrivateProfileString(string section,
  10:         string key, string def, StringBuilder retVal,
  11:         int size, string filePath);
  12:  
  13:     public void IniWriteValue(string Section, string Key, string Value, string inipath)
  14:     {
  15:         WritePrivateProfileString(Section, Key, Value, inipath);
  16:     }
  17:     public string IniReadValue(string Section, string Key, string inipath)
  18:     {
  19:         StringBuilder temp = new StringBuilder(255);
  20:         int i = GetPrivateProfileString(Section, Key, "", temp, 255, inipath);
  21:         return temp.ToString();
  22:     }
  23:     public string IniReadValue(string Section, string Key, string Value, string inipath)
  24:     {
  25:         StringBuilder temp = new StringBuilder(255);
  26:         int i = GetPrivateProfileString(Section, Key, Value, temp, 255, inipath);
  27:         return temp.ToString();
  28:     }
  29: }


使用 SetupIni :


   1: private void LoadIniFile()
   2: {
   3:     SetupIni ini = new SetupIni();
   4:     string iniFilename = Path.Combine(Application.StartupPath, "TVBS_SSNMP.ini");
   5:     try
   6:     {
   7:         if (File.Exists(iniFilename))
   8:         {
   9:             IP_AsiGuard1 = ini.IniReadValue("AsiGuard1", "IP", IP_AsiGuard1, iniFilename);
  10:             IP_AsiGuard2 = ini.IniReadValue("AsiGuard2", "IP", IP_AsiGuard2, iniFilename);
  11:             IP_IPGuard1 = ini.IniReadValue("IPGuard1", "IP", IP_IPGuard1, iniFilename);
  12:  
  13:             ini.IniWriteValue("AsiGuard1", "IP", IP_AsiGuard1, iniFilename);
  14:             ini.IniWriteValue("AsiGuard2", "IP", IP_AsiGuard2, iniFilename);
  15:             ini.IniWriteValue("IPGuard1", "IP", IP_IPGuard1, iniFilename);
  16:         }
  17:         else //.. 建立新 INI_File
  18:         {
  19:             ini.IniWriteValue("AsiGuard1", "IP", IP_AsiGuard1, iniFilename);
  20:             ini.IniWriteValue("AsiGuard2", "IP", IP_AsiGuard2, iniFilename);
  21:             ini.IniWriteValue("IPGuard1", "IP", IP_IPGuard1, iniFilename);
  22:         }
  23:     }
  24:     catch (Exception)
  25:     {
  26:         // throw;
  27:     }
  28:     finally
  29:     {
  30:         // .......
  31:     }
  32: }

2012年7月17日 星期二

DateTime 的運算

DateTime 與TimeSpan

DateTime 和 TimeSpan 實值型別的差別在於 DateTime 表示時間的一瞬間,而 TimeSpan 則表示時間間隔。 例如,這表示您可以將兩個 DateTime 的執行個體相減,以取得表示兩個執行個體之間時間間隔的 TimeSpan 物件。 或者,您可以將正的 TimeSpan 加上目前的 DateTime,以取得表示未來日期的 DateTime 值。

您可以從 DateTime 物件中加入或減去時間間隔。 時間間隔可以是正數或負數、可以用類似刻度或秒數的單位來表示,也可以表示為 TimeSpan 物件。

 

DateTime 可做增減,如下方法:

名稱

說明

Add

傳回新的 DateTime,這是將這個執行個體的值加上指定之 TimeSpan 值的結果。

AddDays

傳回新的 DateTime,這是將這個執行個體的值加上指定之天數的結果。

AddHours

傳回新的 DateTime,這是將這個執行個體的值加上指定之時數的結果。

AddMilliseconds

傳回新的 DateTime,這是將這個執行個體的值加上指定之毫秒數的結果。

AddMinutes

傳回新的 DateTime,這是將這個執行個體的值加上指定之分鐘數的結果。

AddMonths

傳回新的 DateTime,這是將這個執行個體的值加上指定之月數的結果。

AddSeconds

傳回新的 DateTime,這是將這個執行個體的值加上指定之秒數的結果。

AddTicks

傳回新的 DateTime,這是將這個執行個體的值加上指定之刻度數的結果。

AddYears

傳回新的 DateTime,這是將這個執行個體的值加上指定之年數的結果。

 

標準日期和時間格式字串

Console.WriteLine(thisDate.ToString("d"));
Console.WriteLine("The current date and time: {0:MM/dd/yy H:mm:ss zzz}",thisDate2);



































































格式規範


說明


範例


"d"


簡短日期模式。

詳細資訊:簡短日期 ("d") 格式規範


6/15/2009 1:45:30 PM -> 6/15/2009 (en-US)

6/15/2009 1:45:30 PM -> 15/06/2009 (fr-FR)

6/15/2009 1:45:30 PM -> 2009/06/15 (ja-JP)


"D"


完整日期模式。

詳細資訊:完整日期 ("D") 格式規範


6/15/2009 1:45:30 PM -> Monday, June 15, 2009 (en-US)

6/15/2009 1:45:30 PM -> 15 июня 2009 г. (ru-RU)

6/15/2009 1:45:30 PM -> Montag, 15. Juni 2009 (de-DE)


"f"


完整日期/時間模式 (簡短時間)。

詳細資訊:完整日期簡短時間 ("f") 格式規範


6/15/2009 1:45:30 PM -> Monday, June 15, 2009 1:45 PM (en-US)

6/15/2009 1:45:30 PM -> den 15 juni 2009 13:45 (sv-SE)

6/15/2009 1:45:30 PM -> Δευτέρα, 15 Ιουνίου 2009 1:45 μμ (el-GR)


"F"


完整日期/時間模式 (完整時間)。

詳細資訊:完整日期完整時間 ("F") 格式規範


6/15/2009 1:45:30 PM -> Monday, June 15, 2009 1:45:30 PM (en-US)

6/15/2009 1:45:30 PM -> den 15 juni 2009 13:45:30 (sv-SE)

6/15/2009 1:45:30 PM -> Δευτέρα, 15 Ιουνίου 2009 1:45:30 μμ (el-GR)


"g"


一般日期/時間模式 (簡短時間)。

詳細資訊:一般日期簡短時間 ("g") 格式規範


6/15/2009 1:45:30 PM -> 6/15/2009 1:45 PM (en-US)

6/15/2009 1:45:30 PM -> 15/06/2009 13:45 (es-ES)

6/15/2009 1:45:30 PM -> 2009/6/15 13:45 (zh-CN)


"G"


一般日期/時間模式 (完整時間)。

詳細資訊:一般日期完整時間 ("G") 格式規範


6/15/2009 1:45:30 PM -> 6/15/2009 1:45:30 PM (en-US)

6/15/2009 1:45:30 PM -> 15/06/2009 13:45:30 (es-ES)

6/15/2009 1:45:30 PM -> 2009/6/15 13:45:30 (zh-CN)


"M"、"m"


月/日模式。

詳細資訊:月 ("M"、"m") 格式規範


6/15/2009 1:45:30 PM -> June 15 (en-US)

6/15/2009 1:45:30 PM -> 15. juni (da-DK)

6/15/2009 1:45:30 PM -> 15 Juni (id-ID)


"O"、"o"


來回日期/時間模式。

詳細資訊:來回 ("O"、"o") 格式規範


6/15/2009 1:45:30 PM -> 2009-06-15T13:45:30.0900000


"R"、"r"


RFC1123 模式。

詳細資訊:RFC1123 ("R"、"r") 格式規範


6/15/2009 1:45:30 PM -> Mon, 15 Jun 2009 20:45:30 GMT


"s"


可排序日期/時間模式。

詳細資訊:可排序 ("s") 格式規範


6/15/2009 1:45:30 PM -> 2009-06-15T13:45:30


"t"


簡短時間模式。

詳細資訊:簡短時間 ("t") 格式規範


6/15/2009 1:45:30 PM -> 1:45 PM (en-US)

6/15/2009 1:45:30 PM -> 13:45 (hr-HR)

6/15/2009 1:45:30 PM -> 01:45 م (ar-EG)


"T"


完整時間模式。

詳細資訊:完整時間 ("T") 格式規範


6/15/2009 1:45:30 PM -> 1:45:30 PM (en-US)

6/15/2009 1:45:30 PM -> 13:45:30 (hr-HR)

6/15/2009 1:45:30 PM -> 01:45:30 م (ar-EG)


"u"


國際可排序日期/時間模式。

詳細資訊:國際可排序 ("u") 格式規範


6/15/2009 1:45:30 PM -> 2009-06-15 20:45:30Z


"U"


國際完整日期/時間模式。

詳細資訊:國際完整 ("U") 格式規範


6/15/2009 1:45:30 PM -> Monday, June 15, 2009 8:45:30 PM (en-US)

6/15/2009 1:45:30 PM -> den 15 juni 2009 20:45:30 (sv-SE)

6/15/2009 1:45:30 PM -> Δευτέρα, 15 Ιουνίου 2009 8:45:30 μμ (el-GR)


"Y", "y"


年月模式。

詳細資訊:年月 ("Y") 格式規範


6/15/2009 1:45:30 PM -> June, 2009 (en-US)

6/15/2009 1:45:30 PM -> juni 2009 (da-DK)

6/15/2009 1:45:30 PM -> Juni 2009 (id-ID)


 








































   建


   說


DateTime(Int64)


DateTime 類別的新執行個體初始化為刻度的指定數目。


DateTime(Int64, DateTimeKind)


DateTime 結構的新執行個體初始化為指定的刻度數以及 Coordinated Universal Time (UTC) 或本地時間。


DateTime(Int32, Int32, Int32)


DateTime 結構的新執行個體初始化為指定的年、月和日。


DateTime(Int32, Int32, Int32, Calendar)


DateTime 結構的新執行個體初始化為指定之月曆的指定之年、月和日。


DateTime(Int32, Int32, Int32, Int32, Int32, Int32)


DateTime 結構的新執行個體初始化為指定的年、月、日、時、分和秒。


DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)


DateTime 結構的新執行個體初始化為指定之年、月、日、時、分、秒以及 Coordinated Universal Time (UTC) 或本地時間。


DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)


DateTime 結構的新執行個體初始化為指定之月曆的指定之年、月、日、時、分和秒。


DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)


DateTime 結構的新執行個體初始化為指定的年、月、日、時、分、秒和毫秒 (Millisecond)。


DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)


DateTime 結構的新執行個體初始化為指定之年、月、日、時、分、秒、毫秒以及 Coordinated Universal Time (UTC) 或本地時間。


DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)


DateTime 結構的新執行個體初始化為指定之月曆的指定之年、月、日、時、分、秒和毫秒。


DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)


DateTime 結構的新執行個體初始化為指定之日曆的指定之年、月、日、時、分、秒、毫秒以及 Coordinated Universal Time (UTC) 或本地時間。


 
























































屬性


  說       明


Date


取得這個執行個體的日期部分。


Day


取得由這個執行個體表示的月份天數。


DayOfWeek


取得由這個執行個體表示的一週天數。


DayOfYear


取得由這個執行個體表示的一年天數。


Hour


取得這個執行個體所表示日期的小時部分。


Kind


取得值,指出這個執行個體表示的時間是根據本地時間、Coordinated Universal Time (UTC),或兩者皆非。


Millisecond


取得這個執行個體所表示日期的毫秒部分。


Minute


取得這個執行個體所表示日期的分鐘部分。


Month


取得這個執行個體所表示日期的月份部分。


Now


取得 DateTime 物件,此物件會設定為這部電腦上目前的日期和時間,以本地時間表示。


Second


取得這個執行個體所表示日期的秒數部分。


Ticks


取得表示這個執行個體日期和時間的刻度數目。


TimeOfDay


取得這個執行個體的一天時間。


Today


取得目前的日期。


UtcNow


取得 DateTime 物件,此物件會設定為這部電腦上目前的日期和時間,以 Coordinated Universal Time (UTC) 表示。


Year


取得這個執行個體所表示日期的年份部分。


 


































  運


  說


Addition


將指定的時間間隔加上指定的日期和時間,產生新的日期和時間。


Equality


判斷 DateTime 的兩個指定之執行個體是否相等。


GreaterThan


判斷某個指定的 DateTime 是否大於另一個指定的 DateTime


GreaterThanOrEqual


判斷某個指定的 DateTime 是否大於或等於另一個指定的 DateTime


Inequality


判斷 DateTime 的兩個指定之執行個體是否不相等。


LessThan


判斷某個指定的 DateTime 是否小於另一個指定的 DateTime


LessThanOrEqual


判斷某個指定的 DateTime 是否小於或等於另一個指定的 DateTime


Subtraction(DateTime, DateTime)


將另一個指定的日期和時間減去指定的日期和時間,並傳回時間間隔。


Subtraction(DateTime, TimeSpan)


將指定的日期和時間減去指定的時間間隔,並傳回新的日期和時間。


 


資料參考來源 : MSDN http://msdn.microsoft.com/zh-tw/library/8kb3ddd4.aspx

2012年7月10日 星期二

C# 概念網

C#

Array    Class    Collections    File    String    .NET    Algorithm     ASP.NETCast    Compression    Data    Delegate    Directive    Enum     Exception    IfInterface    Keyword    LINQ    Loop    Method    Number    Regex    SortStringBuilder    Struct    Switch    Time    Value    Windows

 

參考資料來源:  http://www.dotnetperls.com/


參考資料來源 :  C# Tutorial and source code

An overview of Microsoft C#