- 參考資料
[STAThread]
static void Main()
{
var wi = WindowsIdentity.GetCurrent();
var wp = new WindowsPrincipal(wi);
if (!wp.IsInRole(WindowsBuiltInRole.Administrator))
{
var processInfo = new ProcessStartInfo();
// The following properties run the new process as administrator
processInfo.UseShellExecute = true;
processInfo.FileName = Application.ExecutablePath;
processInfo.Verb = "runas";
// Start the new process
try
{
Process.Start(processInfo);
}
catch (Exception ex)
{
// The user did not allow the application to run as administrator
MessageBox.Show("Sorry, this application must be run as Administrator.\n" + ex.Message);
}
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
C# 6.0 dictionary 初始化的新寫法
var dict = new Dictionary<string, int>
{
["one"] = 1,
["two"] = 2,
["three"] = 3
};
需求: URL 輸入 http://xxxx/yyy 轉成 http://xxxx/yyy.html
最近趕流行,用 vue 寫了一個 SAP(Single application page),html網頁在 MVC 的架構下可成功執行,但我就是想讓使用者操作上更方便,只要輸入網址 route 就直接跳轉到此 html 頁面。如果照原本 MapHttpRoute的方式會造成 .NET 出現找不到 Controller 的問題。最後發現不用 MapHttpRoute 去解此問題,使用MapPageRoute才是正解。
// Web API routes
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
以下增加於 Global.asax.cs
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
GlobalConfiguration.Configure(WebApiConfig.Register);
// 新增加以下 route
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"yyy", "~/yyy.html");
}
}
<compilation debug="true" targetFramework="4.5.2">
<buildProviders >
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
<add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders >
</compilation>
手上有個泰國的生產系統小案子,
雖說是個小案子但也算是跨國專案,
我遇到除了語言不同之外的電腦系統文化差異,
平常我們使用電腦的日期格式一般主要為2018/5/10,
美國系統的國家則是 5/10/2018,也就是 MM/dd/yyyy。
但泰國的使用曆別是一個本人未曾耳聞的佛曆,
就是釋迦牟尼佛滅度(涅槃)當年度為計算基準,
西元2018年就是泰曆/佛曆2561年,年度不同就算了還有四年一次閏年。
真要弄起來那就頭疼了。
不過在網路上找到一個小解法,
執行的程式強制指定CultureInfo,
為了這個設定我可是弄到凌晨三時多,真是工程師的悲哀。
以下為其語法,分享給作泰國專案的網友們。
string theCultureString = “zh-TW”; // 台灣南波萬
System.Globalization.CultureInfo ci = new CultureInfo(theCultureString);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
加碼再作個記錄,此專案使用到一個強大元件:GrapeCity Spread。
雖然事前設定CurrentThread的CultureInfo,
但日期資料在輸入時仍有被我佛慈悲的佛曆帶著走,
還是得強迫在 cell 設定CultureInfo處理日期。
// 強制使用台灣 Culture, 避免泰國佛曆造成衝突
string theCultureString = "zh-TW"
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(theCultureString); datecell.SetCurrentCulture(ci);
開發系統時,使用者常常會問可以知道資料是被那個員工更新的,更新什麼內容嗎?通常我都會回答系統會記錄Log時間,資料異動者都查詢的到,但是異動那一個欄位就沒辦法了,實在也不想為這個很少使用的功能再大費周張去寫程式作欄位比較再存到log去。
今天碰巧查個MSSQL的資料,才發現MSSQL 2008 就有類似的功能了,還不用寫程式作些設定就可以達到這個功能,以下就是這個CDC (Change Data Capture) 的簡單實測記錄。
-- CDC = Change Data Capture
-- 執行 CDC, sp_cdc_enable_table 為 enable, sp_cdc_disable_table 為 disable
EXEC sys.sp_cdc_enable_table
@source_schema = N'dbo',
@source_name = N'items',
@role_name = NULL
GO
-- 觀察修改前、後差異
select * from cdc.dbo_items_CT
-- 目前 DB 中有那些 table 被 tracking
SELECT [name], is_tracked_by_cdc FROM sys.tablesGO
-- CCD 設定多久會清除, 預設為 4320 分鐘或 3 天, 最大值為 52494800 (100 年)
select retention, threshold
from msdb.dbo.cdc_jobs
where database_id = db_id()
and job_type = N'cleanup'