No Picture
C#

[C#] 如何連接UNC path

2016-03-22 C.H. Ling 0

在.net environment, 本身不支援UNC path, 若加上user name /password 設定, 總會throw exception. 網上找來一個class, 它是call windows dll 去進行連結. 若在windows environment 的話, 不失為一個好方法.

No Picture
C#

[C#] 如何為貨幣加上 thoudenson separator

2016-03-22 C.H. Ling 0

在顯示貨幣時, 每隔三個位, 都須要加一個”,”分隔, 在C# 可以自行implement 如下: public static string ConvertToCurrency(double value) { string result = String.Format(“{0:#,##0.##00}”,value); return result; }  

No Picture
C#

[C#] 如何將string encode / decode

2016-03-22 C.H. Ling 0

部份內容須要儲存或傳送, 若plain text 的話會是一個考量, 故須要將它encode. public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); } public static string Base64Decode(string base64EncodedData) { var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); }  

No Picture
C#

[C#] 如何發出電郵

2016-03-22 C.H. Ling 1

在send email 前, 先講如何制造email object. 這裡用了一粒class 去包著須要send的內容. 故砌真. object 時, 這個method 可以定義為adapter 的implementation. private MailMessage createMailMessage(EMAIL email) { MailMessage mail = new MailMessage() { From = new MailAddress(email.FROM_EMAIL_ADDRESS), }; mail.To.Add(new MailAddress(email.TO_EMAIL_ADDRESS)); if (!string.IsNullOrEmpty(email.CC_EMAIL_ADDRESS)) mail.CC.Add(new MailAddress(email.CC_EMAIL_ADDRESS)); […]

No Picture
ASP.net MVC

[ASP.net MVC] 如何upload file並經controller 放於Network storage

2016-03-22 C.H. Ling 0

View: 留意須要自行定義encType作multipart/form-data, 不然的話controller 不會接到file value. @using (Html.BeginForm(“CreatePost”, “Email”,FormMethod.Post, new { enctype= “multipart/form-data”, id=”formSendEmail” })) { @Html.AntiForgeryToken() <div class=”form-horizontal”> <input type=”file” id=”attachments” name=”attachments[]” multiple /> <div class=”form-group”> <div class=”col-md-offset-2 col-md-10″> <input type=”submit” value=”Create with file” class=”btn […]

No Picture
C#

[C#] 如何產生Random string

2016-03-22 C.H. Ling 0

若在concurrency中, 有時須要產生一堆random number去做, 但工作上, 要制做一條unique path, 除了用primary key 外, timestamp + random number 亦是一個好選擇. 為了更精準 (或者無聊), 便嘗試了random string. private string RandomString(int size) { Random random = new Random((int)DateTime.Now.Ticks); StringBuilder builder = new StringBuilder(); char ch; […]

No Picture
ASP.net MVC

[ASP.net MVC] 如何自建User control

2016-03-16 C.H. Ling 0

在建構框架 (framework)時, 會有機會須要重覆使用一堆指定的 user control. 其實透過 extend HtmlHelper, 便可以將coding變得簡單, 將來修改時亦只須要修改一部份便可. 在這裡, 用了一個Bootstrap control 做例子. public static class HtmlHelperExtend { public static MvcHtmlString DateTimePicker(this HtmlHelper helper, string divDayTimePickerId, string txtBoxdayTimePickerId, DateTime? value, string labelId, string labelText=”Label […]

No Picture
ASP.net MVC

[ASP.net MVC] Session Expire Attribute

2016-03-10 C.H. Ling 0

當session timeout時, 若不作處理, 運作時有機會throw exception. 為應對此情況, 可以自訂一個Attribute, 當controller / action call 時, 便會先行attribute. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Test.Attributes { public class SessionExpireAttribute : ActionFilterAttribute { public override void […]

1 2 3 4