2021年6月28日 星期一

判斷char 是否為UTF16

bool isUTF16(char value)
            {
                return Convert.ToInt32(value) >= Convert.ToInt32("d800", 16) && Convert.ToInt32(value) <= Convert.ToInt32("dfff", 16);
            }

※中文字串透過 ToArray() 拆解成 char 陣列時,特殊字會用UTF16處理,一個中文字會變成兩個char(顯示為\uxxxx),並使用d800~dfff的code point

2021年6月22日 星期二

windows form AutoScaleMode 和 windows 縮放比例的關係

inherit,none : 一起放大縮小,可能會造成視窗某些區域超出螢幕之外

dpi,font : 一起放大縮小,但視窗高度比例較小造成內部元件可能會超過底部

執行檔右鍵 > 內容 > 相容性 > 變更高DPI設定 改成下面設定 : 只有字型一起放大縮小,基本上不太會有問題,是目前最好的解法



2021年6月15日 星期二

連線字串動態設定密碼

建立 xxxEntities 的擴充class,加入方法 : CreateDbContext
public partial class xxxEntities : DbContext
    {
        private xxxEntities(string connectionString)
        : base(connectionString)
        {
        }

        public static xxxEntities CreateDbContext()
        {
            var efbuilder = new EntityConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["xxxEntities"].ConnectionString);
            var sqlbuilder = new SqlConnectionStringBuilder(efbuilder.ProviderConnectionString);
            sqlbuilder.Password = "xxx";
            efbuilder.ProviderConnectionString = sqlbuilder.ConnectionString;
            return new xxxEntities(efbuilder.ConnectionString);
        }
    }

使用方式
using (var db=xxxEntities.CreateDbContext())

2021年6月9日 星期三

判斷檔案是否鎖定中

例如正在寫入中,檔案還不完整

public static bool IsFileLocked(this System.IO.FileInfo file)
    {
        System.IO.FileStream stream = null;
        try
        {
            stream = file.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None);
        }
        catch 
        {
            //the file is unavailable because it is:
            //still being written to
            //or being processed by another thread
            //or does not exist (has already been processed)
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
        //file is not locked
        return false;
    }

2021年6月8日 星期二

使用 jquery 操作 radio list

<label><input type="radio" name="xxx" value="xxx" />xxx</label>
<label><input type="radio" name="xxx" value="xxx" />xxx</label>


取得選取值
$("input:radio[name='xxx']:checked").val()

設定選取值
$(`input:radio[name='xxx'][value='${xxx}']`).prop('checked', true);

清空選取值
$("input:radio[name='xxx']").prop('checked',false);

2021年6月3日 星期四

判斷兩個區間是否重疊

假設有兩個區間 a,b
分別有start 和end
重疊會有四種情況
1: a.start<=b.start and a.end>b.start
2: b.end>a.start and b.end<=a.end
3: a.start<=b.start and a.end>=b.end
4: b.start<=a.start and b.end>=a.end
簡易判斷法 : max(a.start,b.start)<min(a.end,b.end)

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...