2013年5月30日 星期四

C# HttpListener 範例

建立Console專案.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpListener listener = new HttpListener();
            try
            {
                listener.Prefixes.Add("http://*:8055/"); //要監聽的URL範圍
                listener.Start(); //開始監聽端口,接收客户端請求
                Console.WriteLine("Listening");
                while (true)
                {
                    //獲取一個客户端請求為止
                    HttpListenerContext context = listener.GetContext();
                    //將其處理過程放入線程池(Pool)
                    System.Threading.ThreadPool.QueueUserWorkItem(ProcessHttpClient, context);
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }
            finally
            {
                listener.Stop(); //關閉HttpListener
            }
        }

        //客户請求處理
        static void ProcessHttpClient(object obj)
        {
            HttpListenerContext context = obj as HttpListenerContext;
            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;
            //string s = request.QueryString["command"];
            //do something as you want
            string responseString = string.Format(" {0}", DateTime.Now);
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);

            //關閉輸出流,釋放相應資源。
            output.Close();
        }
    }
}




2013年5月15日 星期三

Ext.Net 學習第一步


資料來源:http://extnet.tumblr.com/post/30985419252/ext-net-ext-net



安裝步驟 & 開始使用 如下:
1.安裝Visual Studio 2005,2008 or 2012 或
   Visual Web Developer Express 2005,2008 or 2010
如果未安裝的,可以裝Visual Web Developer Express 2010,是免費的,且使用上跟Visual Studio沒什麼差異。。
2.安裝.Net Framework 3.5以上版本
3.下載元件檔,可以至http://www.ext.net/download/下載你所要的版本
↓將下載回來的檔案解開來,我習慣開個asp.net 的資料夾放這些元件,待回兒會使用到
4.開啟專案如附圖
檔案→新網站→選擇ASP.NET空網站(空網站比較乾淨,不會有一些雜七雜八的…)
選擇的語言Visual C#(因為Ext.Net都是用C#寫的,所以選C#會比較好,Ext.Net的範例也都是C#,比較沒有轉換的問題)
當然,您要用vb也是可以使用的,但網路上的支援大部份都是C#…
↓加一個頁面進來
↓語言一樣建議是C#,選擇Web Form
這裡我不勾選將程式碼置於各別檔案中,為什麼?
主要是日後要在Ext.Net的討論區發問會比較方便,檔案結構也會比較簡潔….(不會拆成兩個檔.aspx及.cs)
5.將Ext.Net元件安裝到工具箱(只需安裝一次,日後新增的專案就不需要做這個動作了)
↑標籤命名為Ext.Net
↓接下來在Ext.Net按右鍵→選擇項目→.Net Framework元件→點瀏覽→
 選擇剛才解壓縮後放至本機的Ext.Net.dll檔→開啟舊檔
↓預設會勾選Ext.Net的全部元件,直接按”確定”,就ok了
↓元件已經加進來了
6.開始我們第一個Ext.Net的頁面吧
要使用Ext.Net,第一件事,就是加上ResourceManager,不然會有err
↓將元件ResourceManager拉至你的頁面
當你拉好後,你會發現你的方案總管就會自動出現bin目錄,跟一些dll檔
↓接下來測試拉個Window並Run看看吧~
第一次會出現是不是要出現除錯訊息,因為還沒有要放到server上,就按確定就可以了
↓Run的時侯會出現Web.config要設定的訊息
↓我們把這一段設定加上,再重新Run一次看看吧
↓很感動的,我們完成第一隻Ext.Net的程式了
是不是很沒Feel?
你們可以到http://examples.ext.net/找Code,貼到剛才的.aspx上玩看看喔
The End…

2013年3月25日 星期一

[C#] 刪除1天前某個目錄的檔案

            //刪除1天前某個目錄的檔案,減少temp檔佔用空間
            try
            {
                string TmpRootPath= @"C:\temp";
                DirectoryInfo TempInfo = new DirectoryInfo(TmpRootPath);
                FileInfo[] TempArray = TempInfo.GetFiles();
                if (TempArray.Length > 0)
                {
                    for (int i = 0; i < TempArray.Length; i++)
                    {
                        string TempFileName = TempArray[i].Name;
                        DateTime dtTempFileEditTime = TempArray[i].LastWriteTime;
                        TimeSpan ts = DateTime.Now - dtTempFileEditTime;
                        int differenceInDays = ts.Days;
                        if (differenceInDays > 1) File.Delete(TmpRootPath+ TempFileName);
                    }
                }

2012年10月12日 星期五

2012年9月27日 星期四

css下拉式選單被圖蓋掉


用CSS執行下拉式選單,可以參考範例:

1.[CSS]純CSS實作觸碰下拉式選單並適用各瀏覽

2.超贊的純CSS實現的簡約下拉式功能表

實作後,會發現展開下拉選單時,若下方有圖片會被蓋掉,
這時後請修改CSS檔,將選單外圍容器加上  position:relative z-index:99 即可。