2008年5月27日 星期二

C# 特定安全性建議

http://msdn.microsoft.com/zh-tw/library/ms173195(VS.80).aspx

Visual C# 語言概念

http://msdn.microsoft.com/zh-tw/library/kx37x362(VS.80).aspx

2008年5月26日 星期一

C# 運算子

下表根據運算子所執行的運算類型,將運算子分為幾類。每個分類都是依照優先順序列出。

主要
 x.y、f(x)、a[x]、x++、x--、new、typeof、checked、unchecked

一元 +、-、!、~、++x、--x、(T)x

算術 (乘法類) *, /, %

算術 (加法類) +, -

移位 <<, >>

關係和型別測試 <, >, <=, >=, is, as

相等 ==, !=

邏輯 (依照優先順序排列) &, ^, |

條件 (依照優先順序排列) &&, ||, ?:

設定 =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

C# 使用字串

逸出字元

像是 "\n" (新增一行) 和 "\t" (定位鍵) 的逸出字元可包含在字串中。

程式碼行:
string hello = "Hello\nWorld!";

產出
Hello

World!

============================
如果要包含反向斜線,在反向斜線之前必須有另一個反向斜線。
下列字串:
string filePath = "\\\\My Documents\\";

產出
\\My Documents\

====================================
@ 符號

@ 符號會告訴字串建構函式忽略逸出字元和分行符號。
下列兩個字串是完全相同的:
string p1 = "\\\\My Documents\\My Files\\";
string p2 = @"\\My Documents\My Files\";

================================================
要在字串中搜尋字串,請使用 IndexOf()。如果找不到搜尋字串,IndexOf() 會傳回 -1,否則,會傳回第一個發生位置之以零起始的索引。

string s9 = "Battle of Hastings, 1066";

System.Console.WriteLine(s9.IndexOf("Hastings")); // outputs 10
System.Console.WriteLine(s9.IndexOf("1967")); // outputs -1

==================================================
將字串分隔為子字串

將字串分隔為子字串 (例如,將句子分隔為個別文字) 是常見的程式設計工作。Split() 方法使用分隔符號 (例如,空白字元) 的 char 陣列,並且傳回子字串陣列。您可以用 foreach 存取這個陣列,如下所示:

char[] delimit = new char[] { ' ' };
string s10 = "The cat sat on the mat.";
foreach (string substr in s10.Split(delimit))
{
System.Console.WriteLine(substr);
}

產出
The

cat

sat

on

the

mat.


=============================
http://msdn.microsoft.com/zh-tw/library/ms228362(VS.80).aspx

C# → Main() 傳回值

Main 方法可以屬於型別 void:

static void Main()
{
//...
}

它也可以傳回 int:

static int Main()
{
//...
return 0;
}

========MainReturnValue.cs===================================================

class MainReturnValue
{
static int Main()
{
//...
return 0;
}
}

===============text.bat=============================================

rem test.bat
@echo off
MainReturnValue
@if "%ERRORLEVEL%" == "0" goto good

:fail
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end

:good
echo Execution Succeded
echo return value = %ERRORLEVEL%
goto end

:end

=====================================================

http://msdn.microsoft.com/zh-tw/library/0fwzzxz2(VS.80).aspx

2008年5月22日 星期四

M$N 去廣告

「hosts」檔案裡的最下面加入下面兩行字:

127.0.0.1 rad.msn.com
127.0.0.1 rad.live.com


Win 98/ME 的修改位置:「C:\WINDOWS」資料夾中的「hosts」檔案。


Windows 2000 : 「C:\WINNT\SYSTEM32\DRIVERS\ETC」資料夾中的「hosts」檔案。


Windows XP:「C:\WINDOWS\system32\drivers\etc」 資料夾中的「hosts」檔案。


Windows Vista:「C:\Windows\System32\drivers\etc」資料夾中的「hosts」檔案。

2008年5月21日 星期三

C# 中的進制轉換

int iNum = 253;
string strResult;

strResult = Convert.ToString(iNum, 2);
// 結果:11111101

strResult = Convert.ToString(iNum, 8);
// 結果:375

strResult = Convert.ToString(iNum, 10);
// 結果:253

strResult = Convert.ToString(iNum, 16);
// 結果:fd

C#字串輸出格式控制

C#的String.Format舉例

stringstr1 =string.Format("{0:N1}",56789); //result: 56,789.0
stringstr2 =string.Format("{0:N2}",56789); //result: 56,789.00
stringstr3 =string.Format("{0:N3}",56789); //result: 56,789.000
stringstr8 =string.Format("{0:F1}",56789); //result: 56789.0
stringstr9 =string.Format("{0:F2}",56789); //result: 56789.00
stringstr11 =(56789 / 100.0).ToString("#.##"); //result: 567.89
stringstr12 =(56789 / 100).ToString("#.##"); //result: 567

C 或 c
貨幣
Console.Write("{0:C}", 2.5); //$2.50
Console.Write("{0:C}", -2.5); //($2.50)

D 或 d
十進位數字
Console.Write("{0:D5}", 25); //00025

E 或 e
科學型
Console.Write("{0:E}", 250000); //2.500000E+005

F 或 f
固定點
Console.Write("{0:F2}", 25); //25.00
Console.Write("{0:F0}", 25); //25

G 或 g
常規
Console.Write("{0:G}", 2.5); //2.5

N 或 n
數字
Console.Write("{0:N}", 2500000); //2,500,000.00

X 或 x
十六進位
Console.Write("{0:X}", 250);

/******************************************************************************/
ASP.NET設置資料格式與String.Format使用總結(引)
{0:d} YY-MM-DD

{0:p} 百分比00.00%

{0:N2} 12.68

{0:N0} 13

{0:c2} $12.68

{0:d} 3/23/2003

{0:T} 12:00:00 AM

{0:男;;女}

DataGrid-資料格式設置運算式

資料格式設置運算式

.NET Framework 格式設置運算式,它在資料顯示在列中之前先應用於資料。此運算式由可選靜態文本和用以下格式表示的格式說明符組成:

{0:format specifier}

零 是參數索引,它指示列中要格式化的資料元素;因此,通常用零來指示第一個(且唯一的)元素。format specifier 前面有一個冒號 (:),它由一個或多個字母組成,指示如何格式化資料。可以使用的格式說明符取決於要格式化的資料類型:日期、數位或其他類型。下表顯示了不同資料類型的 格式設置運算式的示例。有關格式設置運算式的更多資訊,請參見格式化類型。

格式設置運算式


應用於此資料類型


說明


Price: {0:C}


numeric/decimal


顯示“Price:”,後跟以貨幣格式表示的數字。貨幣格式取決於通過 Page 指令或 Web.config 檔中的區域性屬性指定的區域性設置。


{0:D4}


integer(不能和小數一起使用。)


在由零填充的四個字元寬的欄位中顯示整數。


{0:N2}%


numeric


顯示精確到小數點後兩位元的數字,後跟“%”。


{0:000.0}


numeric/decimal


四捨五入到小數點後一位元的數字。不到三位元的數字用零填充。


{0:D}


date/datetime


長日期格式(“Thursday, August 06, 1996”)。日期格式取決於頁或 Web.config 檔的區域性設置。


{0:d}


date/datetime


短日期格式(“12/31/99”)。


{0:yy-MM-dd}


date/datetime


用數字的年-月-日表示的日期(96-08-06)。

唯讀

當此列處於編輯模式時,該列中的資料是否顯示在可編輯的控制項中。

2006-02-22 | asp.net資料格式的Format-- DataFormatString

我 們在呈現資料的時候,不要將未經修飾過的資料呈現給使用者。例如金額一萬元,如果我們直接顯示「10000」,可能會導致使用者看成一千或十萬,造成使用 者閱讀資料上的困擾。若我們將一萬元潤飾後輸出為「NT$10,000」,不但讓使比較好閱讀,也會讓使用者減少犯錯的機會。
下列畫面為潤飾過的結果:
上述資料除了將DataGrid Web 控制項以顏色來區隔記錄外,最主要將日期、單價以及小計這三個計欄位的資料修飾的更容易閱讀。要修飾欄位的輸出,只要設定欄位的DataFormatString 屬性即可;其使用語法如下:

DataFormatString="{0:格式字串}"

我 們知道在DataFormatString 中的 {0} 表示資料本身,而在冒號後面的格式字串代表所們希望資料顯示的格式;另外在指定的格式符號後可以指定小數所要顯示的位元數。例如原來的資料為 「12.34」,若格式設定為 {0:N1},則輸出為「12.3」。其常用的數值格式如下表所示:

格式字串 資料 結果
"{0:C}" 12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 00012345
"{0:E}" 12345.6789 1234568E+004
"{0:E10}" 12345.6789 1.2345678900E+004
"{0:F}" 12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789 12345.6789
"{0:G7}" 123456789 1.234568E8
"{0:N}" 12345.6789 12,345.68
"{0:N4}" 123456789 123,456,789.0000
"Total: {0:C}" 12345.6789 Total: $12345.68

其常用的日期格式如下表所示:

格式 說明 輸出格式
d 精簡日期格式 MM/dd/yyyy
D 詳細日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時間格式
(long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M 月日格式 MMMM dd
s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t 精簡時間格式 HH:mm
T 詳細時間格式 HH:mm:ss

string.format格式結果

String.Format

(C) Currency: . . . . . . . . ($123.00)

(D) Decimal:. . . . . . . . . -123

(E) Scientific: . . . . . . . -1.234500E+002

(F) Fixed point:. . . . . . . -123.45

(G) General:. . . . . . . . . -123

(N) Number: . . . . . . . . . -123.00

(P) Percent:. . . . . . . . . -12,345.00 %

(R) Round-trip: . . . . . . . -123.45

(X) Hexadecimal:. . . . . . . FFFFFF85

(d) Short date: . . . . . . . 6/26/2004

(D) Long date:. . . . . . . . Saturday, June 26, 2004

(t) Short time: . . . . . . . 8:11 PM

(T) Long time:. . . . . . . . 8:11:04 PM

(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM

(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM

(g) General date/short time:. 6/26/2004 8:11 PM

(G) General date/long time: . 6/26/2004 8:11:04 PM

(M) Month:. . . . . . . . . . June 26

(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT

(s) Sortable: . . . . . . . . 2004-06-26T20:11:04

(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)

(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM

(Y) Year: . . . . . . . . . . June, 2004

(G) General:. . . . . . . . . Green

(F) Flags:. . . . . . . . . . Green (flags or integer)

(D) Decimal number: . . . . . 3

(X) Hexadecimal:. . . . . . . 00000003

說明:
String.Format
將指定的 String 中的每個格式項替換為相應物件的值的文本等效項。

例子:

int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帳號是:{0} 。訪問了 {1} 次.", szName, iVisit));

C# 存取修飾詞

存取修飾詞是用於指定成員或型別之宣告存取範圍的關鍵字。
`

*

public
*

protected
*

internal
*

private



下列五種存取範圍層級可以使用存取修飾詞來指定:

public:存取沒有限制。

protected:存取只限於包含類別或衍生自包含類別的型別。

internal
:存取只限於目前的組件。

protected internal
:存取只限於目前的組件或衍生自包含類別的型別。

private:存取只限於包含型別(類別)。


宣告存取範圍 意義

public 存取沒有限制。

protected 存取只限於包含的類別或衍生自包含類別的型別。

internal 存取只限於目前的組件。

protected internal
存取只限於目前的組件或衍生自包含類別的型別。

private 存取只限於包含類別。



http://msdn.microsoft.com/zh-tw/library/wxh6fsc7%28VS.80%29.aspx

C# 使Function傳回多

C# 2.0

List<T>
可以依照索引存取的強型別物件清單。提供搜尋、排序和管理清單的方法。


Iterator
Iterator 是 C# 2.0 中的新功能。Iterator 是一種方法、get 存取子或運算子,它可讓您支援類別或結構中的 foreach 反覆運算,而不需要實行整個 IEnumerable 介面。相反地,您只要提供 Iterator,它會只往返於類別中的資料結構。當編譯器偵測到您的 Iterator 時,它會自動產生 IEnumerable 或 IEnumerable 介面的 Current、MoveNext 和 Dispose 方法。

Iterator 概觀
Iterator 是程式碼區段,會傳回相同型別之按順序排列的值。

Iterator 可以當做方法主體、運算子或 get 存取子使用。

Iterator 程式碼會使用 yield return 陳述式輪流傳回各元素。yield break 則會結束反覆運算。如需詳細資訊,請參閱 yield。

可在類別上實作多個 Iterator。每個 Iterator 必須像任何類別成員一樣擁有唯一名稱,且可以由 foreach 陳述式中的用戶端程式碼叫用,如下所示:foreach(int x in SampleClass.Iterator2){}。

Iterator 的傳回型別必須是 IEnumerable、IEnumerator、IEnumerable 或 IEnumerator。

yield 關鍵字可用來指定傳回的值。當到達 yield return 陳述式時,便會儲存目前的位置。此 Iterator 下一次被呼叫時,就會從這個位置重新執行。

Iterator 特別適合與集合類別搭配使用,因為能夠提供逐一查看像是二元樹等複雜資料結構的方法。

2008年5月20日 星期二

Distributed Transaction Coordinator

SQL Transaction


類別
System.Transactions



服務

需開啟 DTC

Distributed Transaction Coordinator

協調跨越多個資源管理員的交易,比如資料庫、訊息佇列及檔案系統。如果此服務被停止,這些交易將不會發生。如果服務被停用,任何明顯

依存它的服務將無法啟動。

2008年5月19日 星期一

Roadmap of Programming and Development 程式開發的學習藍圖

Roadmap of Programming and Development
程式開發的學習藍圖

作者:朱明中

http://www.microsoft.com/taiwan/msdn/columns/jhu_ming_jhong/rop.htm

BasePage

public class BasePage : System.Web.UI.Page
{
public BasePage():base()
{
}
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
if (this.ValidatePermission() == false)
{
throw new Exception("Not Auth");
}
}
============================================================================
protected virtual bool ValidatePermission()
{
return false;
}
}


public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
protected override bool ValidatePermission()
{
If( User.Name == "維克大隊長")
{
return true;
}
else
{
return false;
}
}
}

2008年5月9日 星期五

C#與VB類別共用

專案裡的App_Code,如果放.cs 和.vb的類別

在Web.config裡面加入下面語法







完整Web.config如下:

















特殊符號

㊊328A ㊋328B ㊌328C ㊍328D ㊎328E ㊏328F
㊐3290 ㊑3291 ㊒3292 ㊓3293 ㊔3294 ㊕3295 ㊖3296 ㊗3297
㊢32A2 ㊣32A3 ㊤32A4 ㊥32A5 ㊦32A6 ㊧32A7 ㊨32A8 ㊩32A9
㊫32AB ㊬32AC ㊭32AD ㊮32AE ㊯32AF
♀2640 ♁2641 ♂2642 ♃2643 ♄2644 ♅2645 ♆2646 ♇2647 ☀2600 ☁2601 ☂2602 ☃2603 ☄2604 ★2605 ☆2606 ☇2607
♈2648 ♉2649 ♊264A ♋264B ♌264C ♍264D
♎264E ♏264F ♐2650 ♑2651 ♒2652 ♓2653
♔2654 ♕2655 ♖2656 ♗2657 ♘2658 ♙2659
♠2660 ♡2661 ♢2662 ♣2663 ♤2664 ♥2665 ♦2666♧2667 ♨2668
♩2669 ♪266A ♫266B ♬266C ♭266D ♮266E♯266F
✁2701 ✂2702✃2703 ✄2704 ✆2706 ✈2708
✉2709 ✐2710 ✑2711✒2712 ✓2713 ✔2714 ✕2715 ✖2716
✗2717✘2718 ✙2719 ✠2720 ✡2721 ✢2722 ✣2723✤2724
✥2725 ✦2726 ✧2727 ✩2729 ✰2730✱2731 ✲2732 ✳2733☈2608 ☉2609 ☊260A ☋260B ☌260C ☍260D ☎260E ☏260F
☑2611 ☒2612 ☓2613
☚261A ☛261B ☜261C ☝261D ☞261E ☟261F
☠2620 ☡2621 ☢2622 ☣2623 ☤2624 ☥2625 ☦2626 ☧2627
☨2628 ☩2629 ☪262A ☫262B ☬262C ☭262D ☮252E ☯262F
☰2630 ☱2631☲2632 ☳2633☴2634 ☵2635 ☶2636☷2637
✴2734 ✵2735 ✶2736✷2737 ✸2738 ✹2739 ✿273F
❀2740 ❁2741 ❂2742❃2743 ❄2744 ❅2745 ❆2746 ❇2747
☸2638 ☹2639 ☺263A ☻263B ☼263C ☽263D ☾263E ☿263F
❡2761❢2762 ❣2763 ❤2764 ❥2765 ❦2766 ❧2767
ⓐ24D0 ⓑ24D1 ⓒ24D2 ⓓ24D3 ⓔ24D4 ⓕ24D5 ⓖ24D6
ⓗ24D7 ⓘ24D8 ⓙ24D9 ⓚ24DA ⓛ24DB ⓜ24DC ⓝ24DD
ⓞ24DE ⓟ24DF ⓠ24E0 ⓡ24E1 ⓡ24E2 ⓣ24E3 ⓤ24E4
❈2748❉2749 ❊274A ❋274B
❍274D ❏274F ❐2750 ❑2751 ❒2752

清除IE記錄資訊

# 清除 IE7 temporary Internet files 紀錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

# 清除 IE7 cookies 紀錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2

# 清除 IE7 瀏覽紀錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1

# 清除 IE7 表單資料
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

# 清除 IE7 已記憶的表單ID與密碼記錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32

# 清除 IE7 全部的記錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255

# 清除 IE7 附加元件所產生的資訊記錄
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351