2013年1月28日 星期一

[C#] 快速比對兩個自訂 List 的差異

首先,要先將該 class 後面補上: IEquatable 並寫覆寫它的 Equals & GetHashCode 這兩支函數
        // 宣告物件的結構
        public class Obj : IEquatable
        {
            //物件名稱
            public string objTitle { get; set; }
            //物件連結
            public string objURL { get; set; }
            //物件所在地
            public string objAddr { get; set; }
            // 物件種類
            public string objKind { get; set; }
            //物件單價
            public string objPrice { get; set; }
            //物件類別
            public string objClass { get; set; }
            //物件坪數
            public string objBuild { get; set; }
            //物件總價
            public string objTotalPrice { get; set; }


            public bool Equals(Obj other)
            {
                //Check whether the compared object is null.
                if (Object.ReferenceEquals(other, null)) return false;

                //Check whether the compared object references the same data.
                if (Object.ReferenceEquals(this, other)) return true;

                //Check whether the properties are equal.
                return objURL.Equals(other.objURL) && 
                    objTitle.Equals(other.objTitle) &&
                    objAddr.Equals(other.objAddr) &&
                    objKind.Equals(other.objKind) &&
                    objPrice.Equals(other.objPrice) &&
                    objClass.Equals(other.objClass) &&
                    objBuild.Equals(other.objBuild) &&
                    objTotalPrice.Equals(other.objTotalPrice);
            }


            public override int GetHashCode()
            {
                // 因為這些變數都是 String 型態的, 所以要判斷是不是 null
                // 如果是其它型態的話, 可以直接 .GetHashCode
                // int hashAge = Age.GetHashCode();

                int hashobjURL = objURL == null ? 0 : objURL.GetHashCode();
                int hashobjTitle = objTitle == null ? 0 : objTitle.GetHashCode();
                int hashobjAddr = objAddr == null ? 0 : objAddr.GetHashCode();
                int hashobjKind = objKind == null ? 0 : objKind.GetHashCode();
                int hashobjPrice = objPrice == null ? 0 : objPrice.GetHashCode();
                int hashobjClass = objClass == null ? 0 : objClass.GetHashCode();
                int hashobjBuild = objBuild == null ? 0 : objBuild.GetHashCode();
                int hashobjTotalPrice = objTotalPrice == null ? 0 : objTotalPrice.GetHashCode();

                // 回傳的部分我還是沒有很理解
                // 我的認知是 GetHashCode() 主要是回傳一個該物件的唯一碼
                // 所以 return 的部分, 就是看如何產生了=_=
                //Calculate the hash code.
                return (hashobjURL + hashobjTitle + hashobjAddr + hashobjKind + hashobjPrice + hashobjClass + hashobjBuild + hashobjTotalPrice);
            }
        }
產生比對:
DiffDataList = ObjDataList.Except(OldDataList).ToList();
別忘了,記得
using System.Linq;
沒有 using 這個 Linq,就沒辦法使用 Except 啦! 所以這應該也算是使用 Linq 去尋找差異吧=_=??

2013年1月13日 星期日

[C#]取得目前執行程式的目錄

抓取 WinForm 應用程式所在的目錄可使用下面方法,此方法會回傳應用程式設定輸出目錄完整路徑。
System.Windows.Forms.Application.StartupPath
抓取 Console 應用程式所在的目錄可使用下面方法。
System.AppDomain.CurrentDomain.BaseDirectory;
抓取 ASP.NET 網頁程式,所在的目錄可用下面方法。
Server.MapPath(".");
若是要包成 Library 呢? 以上皆適用。
System.AppDomain.CurrentDomain.BaseDirectory;
補充 :

protected void Page_Load(object sender, EventArgs e) {
        //傳回傳遞給方法之虛擬路徑的完整實體路徑
        //傳遞給 MapPath 方法的路徑必須是應用程式的相對路徑,而不是絕對路徑。
        Response.Write("Server.MapPath : " + Server.MapPath("~") + "
"); //抓取 ASP.NET 網頁程式,所在的目錄 Response.Write("Server.MapPath : " + Server.MapPath(".") + "
"); //取得 asp.net 應用程式在伺服器上虛擬應用程式根路徑 Response.Write("Request.ApplicationPath : " + Request.ApplicationPath + "
"); //取得目前要求的虛擬路徑 Response.Write("Request.CurrentExecutionFilePath : " + Request.CurrentExecutionFilePath + "
"); //取得目前要求的虛擬路徑,與 CurrentExecutionFilePath 屬性不同,FilePath 並不會反映伺服器端的傳輸。 Response.Write("Request.FilePath : " + Request.FilePath + "
"); //取得目前要求的虛擬路徑 Response.Write("Request.Path : " + Request.Path + "
"); //取得目前執行應用程式之根目錄的實體檔案系統路徑 Response.Write("Request.PhysicalApplicationPath : " + Request.PhysicalApplicationPath + "
"); //取得與要求的 URL 對應之實體檔案系統路徑 Response.Write("Request.PhysicalPath : " + Request.PhysicalPath + "
"); }
輸出結果
來原:http://www.dotblogs.com.tw/atowngit/archive/2009/08/23/10198.aspx

[c#]如何在excel裡加入超連結

Range range = WorkSheet.get_Range(儲存格位置(ex:"I2"), Type.Missing);
Hyperlink hyperLink = (Hyperlink)range.Hyperlinks.Add(range, 超連結要連到哪(string), Type.Missing, Type.Missing, I2要顯示的文字(string));
真實程式範例:
Worksheet ws = (Worksheet)wb.Sheets[1];
...
...
...
for (int i = 0; i < TotalData; i++)
{
    ws.Cells[(i + 2), 1] = ObjDataList[i, 0];
    ws.Cells[(i + 2), 2] = ObjDataList[i, 1];
    ws.Cells[(i + 2), 3] = ObjDataList[i, 2];
    ws.Cells[(i + 2), 4] = ObjDataList[i, 3];
    ws.Cells[(i + 2), 5] = ObjDataList[i, 4];
    ws.Cells[(i + 2), 6] = ObjDataList[i, 5];
    ws.Cells[(i + 2), 7] = ObjDataList[i, 6];
    ws.Cells[(i + 2), 8] = ObjDataList[i, 7];
    ws.Cells[(i + 2), 9] = ObjDataList[i, 8];
    String tmp = (i + 2).ToString();
    Range range = ws.get_Range("I" + tmp, Type.Missing);
    Hyperlink hyperLink = (Hyperlink)range.Hyperlinks.Add(range, ObjDataList[i, 8], Type.Missing, Type.Missing, ObjDataList[i, 8]);
}

2013年1月6日 星期日

[C#] ASCII code to Unicode 、 Unicode to ASCII code

原程式碼:
class Program {
    static void Main( string[] args ) {
        string unicodeString = "This function contains a unicode character pi (\u03a0)";

        Console.WriteLine( unicodeString );

        string encoded = EncodeNonAsciiCharacters(unicodeString);
        Console.WriteLine( encoded );

        string decoded = DecodeEncodedNonAsciiCharacters( encoded );
        Console.WriteLine( decoded );
    }

    static string EncodeNonAsciiCharacters( string value ) {
        StringBuilder sb = new StringBuilder();
        foreach( char c in value ) {
            if( c > 127 ) {
                // This character is too big for ASCII
                string encodedValue = "\\u" + ((int) c).ToString( "x4" );
                sb.Append( encodedValue );
            }
            else {
                sb.Append( c );
            }
        }
        return sb.ToString();
    }

    static string DecodeEncodedNonAsciiCharacters( string value ) {
        return Regex.Replace(
            value,
            @"\\u(?[a-zA-Z0-9]{4})",
            m => {
                return ((char) int.Parse( m.Groups["Value"].Value, NumberStyles.HexNumber )).ToString();
            } );
    }
}
ansi code to uni code
    static string EncodeNonAsciiCharacters( string value ) {
        StringBuilder sb = new StringBuilder();
        foreach( char c in value ) {
            if( c > 127 ) {
                // This character is too big for ASCII
                string encodedValue = "\\u" + ((int) c).ToString( "x4" );
                sb.Append( encodedValue );
            }
            else {
                sb.Append( c );
            }
        }
        return sb.ToString();
    }
uni code to ascii code
    static string DecodeEncodedNonAsciiCharacters( string value ) {
        return Regex.Replace(
            value,
            @"\\u(?[a-zA-Z0-9]{4})",
            m => {
                return ((char) int.Parse( m.Groups["Value"].Value, NumberStyles.HexNumber )).ToString();
            } );
    }

2013年1月2日 星期三

[C#]Webbrowser如何確認網頁載入完成


WebBrowserReadyState的五種狀況



  • Complete 控制項已完成新文件和其所有內容的載入。 
  • Interactive 控制項已載入文件足夠多的部分,可進行有限的使用者互動,例如按一下已顯示的超連結。 
  • Loaded        控制項已載入並初始化新文件,但是尚未收到所有的文件資料。 
  • Loading        控制項正在載入新文件。 
  • Uninitialized 目前未載入任何文件。 


private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{

if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{

//在此範圍中寫下你要在載入完成後做的事件

}

}
原文:http://mming.pixnet.net/blog/post/28321821-%5Bc%23%5Dwebbrowser如何確認網頁載入完成