發表文章

目前顯示的是有「c#」標籤的文章

關於密碼的安全隱私

關於密碼的處理,我的想法是: 1.密碼一定要加密儲存 2.尊重客戶的隱私權 3.讓客戶知道我們一樣重視安全 昨天在客戶那邊處理問題,因為工具稀少,我現場就用記事本寫了這樣的一個程式來測試環境 不過由於需要客戶的密碼,所以我請客戶將密碼打入一個文字檔中 並向他強調,我不能看到他的密碼,所以請不要讓我看到 我從頭到尾都在他旁邊操作,也証明我沒有機會看到密碼 using System; using System.Net; using System.IO; public class test { static void Main() { string userName = "lance" ; string password = new StreamReader ( @"d:\1.txt" ).ReadToEnd().Trim(); string proxystr = "www2.psc.com.tw" ; IWebProxy proxy = new WebProxy (proxystr, 80); proxy.Credentials = new NetworkCredential (userName, password); WebRequest req = WebRequest .Create( "http://www.google.com" ); req.Proxy = proxy; WebResponse res = req.GetResponse(); Stream stream = res.GetResponseStream(); string content = new StreamReader (stream).ReadToEnd(); Console .Write(content); } }

[程式][藝術] 用 xor 來做逆運算

其實程式真的很像藝術,看看下面的程式碼: bool check; // 第一種 if (check) { check = false ; } else { check = true ; } // 第二種 check = (check) ? false : true ; // 第三種 check = !check; // 第四種 check ^= true ; 一樣的句子,透過 xor ( ^= ) 變的多麼簡單,也許上面的式子感覺不太出來,那看看下面的,因為取屬性的寫法變的比較長了,自然 code 也變長了,當然還可能會更長 但一樣透過 xor 就變的很簡潔,像藝術作品一般 // 第一種 if ( this .checkBox1.Checked) { this .checkBox1.Checked = false ; } else { this .checkBox1.Checked = true ; } // 第二種 this .checkBox1.Checked = ( this .checkBox1.Checked) ? false : true ; // 第三種 this .checkBox1.Checked = ! this .checkBox1.Checked; // 第四種 this .checkBox1.Checked ^= true ;

[程式]Linq 未實作的功能

圖片
今天追一個問題,追了半天,原本程式碼如下: MyContext context = new MyContext (); int count = context.Logs.Count(m => m.CreateDate.Value.ToShortDateString() == DateTime .Today.ToShortDateString()); 上面的式子在使用了 linq to Sql, 其中 Logs 資料表中有一個欄位 CreateDate 是 Sql 的 DateTime 型別,在 lamba Expression 中,把它當做 DateTime 型別使用,於是使用了 ToShortDateString 這個方法,編譯一切正常,但執行時,卻發生了例外。 不死心的,將上述的式子改為: MyContext context = new MyContext (); int count = context.Logs.Count(m => m.CreateDate.Value.ToString(" yyyyMMdd ") == DateTime .Today.ToString(" yyyyMMdd ")); 依然出錯,最後只好將式子改為 MyContext context = new MyContext (); int count = context.Logs.Count(m => m.CreateDate.Value.Date == DateTime .Today.Date); 才終於過關,但這一切過程都是 runTime 時才發生問題,原本強調 var 型別的動態性,與強型別編譯的好處,以減少錯誤的發生,但眼前卻顯得一切都不可靠。 最後終於在 msdn 中找到了答案: http://msdn.microsoft.com/en-us/library/bb425822.aspx 節錄最後面一段: System.DateTime Implemented Constructors: Copy Code DateTime(year, month, day) DateTime(year, month, day, hour, minutes, seconds) DateTime(year,...

Zamples, 一個線上貼程式碼並可運算的網站

真的很方便,支援的語言又超多 想試東西又不想自己編譯時也很適合 一定要試試看 Zamples, Inc. Live Online Code Examples.

.Net 用序列化快速完成物件複製

使用序列化就能快速實作物件複製 [Serializable] public class BaseForm : IForm, ICloneable { #region ICloneable Members public object Clone() { MemoryStream ms = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, this); ms.Flush(); ms.Position = 0; object obj = formatter.Deserialize(ms); ms.Close(); return obj; } #endregion } 重點是使用了 System.Runtime.Serialization.Formatters.Binary . BinaryFormatter 這個序列器,使用此序列器,必須加上[ Serializable]這個屬性,因為這個屬性無法被繼承,中間所有的物件也都要加上才行