顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2018年11月18日 星期日

【c#】long = null 無 (long?)null

【c#】long  = null  無  (long?)null

class long? id = null 會發生錯誤

見鬼了  ? 問號不是 無 嗎?怎麼錯誤

原來 null 針對 數字有特別分 ,需要額外轉型別才能 定義 long? 會是 無

class long? id = 轉型 (long?)null  ,就搞定了


2018年7月22日 星期日

【C# Windows Form】 Properties 設定(使用者 User、應用程式 Application)

【C# Windows Form】 Properties 設定(使用者 User、應用程式 Application)

兩者差別 (使用者 User 應用程式 Application)
User Get Set 讀.寫
Applcation Get 唯讀



另外 Save 存檔路徑
Windows 7 開發環境 路徑在:
C:\Users\Administrator\AppData\Local\Colton_Prettyman\XXXXXX\1.1.0.0\user.config

2018年7月18日 星期三

【C# app.config】 手動抓 xml

看完整個 階層...你就瘋了..

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            UserSettingsGroup userSettings = config.GetSectionGroup("userSettings") as UserSettingsGroup;

            ClientSettingsSection items = userSettings.Sections["MyFileSystemWatcher.Properties.Settings"] as ClientSettingsSection;

            SettingElement item = items.Settings.Get("toUser");

            SettingValueElement xml = item.Value;

            String value = xml.ValueXml.InnerText;

直接使用內建:
Properties.Settings.Default.toUser

就不用那麼搞工@@"

2018年2月1日 星期四

【C# NameValueCollection】 針對NameValueCollection 做 LINQ

        public static Dictionary ParseQueryString(string queryString)
        {
            System.Collections.Specialized.NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(queryString);

            var a = from key in nvc.Cast()
                    from value in nvc.GetValues(key)
                    where key != null
                    select new { key, value };

            return a.ToDictionary(pair => pair.key, pair => pair.value); ;
        }

2018年1月24日 星期三

【asp.net C#】在HttpClient的DefaultRequestHeaders上設置Cookie Header

可參考:
http://stackoverflow.com/questions/12373738/how-do-i-set-a-cookie-on-httpclients-httprequestmessage/13287224#13287224

https://d-fens.ch/2016/12/27/howto-set-cookie-header-on-defaultrequestheaders-of-httpclient/

重點兩種

【預設:HttpClientHandler(UseCookies=true)】

HttpClient <--httpclienthandler ookiecontainer="" p="">
cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));

【HttpClient <--httpclienthandler p="">
若是:HttpClientHandler(UseCookies=false) 則是手動加入header

httpClient.DefaultRequestHeaders.Add("Cookie", "auth=ArbitrarySessionToken");

2018年1月17日 星期三

【轉編譯】html javaScript 使用到 encodeURIComponent

【轉編譯】html  java 使用到 encodeURIComponent

範例如下:
javascript
原值: http://tw.yahoo.com/
var a =encodeURIComponent('http://tw.yahoo.com/');

a 的編譯後結果 "http%3A%2F%2Ftw.yahoo.com%2F"

如果程式(C#, PHP,jsp, ....xooxxo)要接收「網頁」傳遞過來的值轉成正常的話
以下方式各程式範例

C#
result1 = System.Web.HttpUtility.UrlDecode(html_data);
String result2 = System.Uri.UnescapeDataString(result1);

Jsp
String htmlurl = req.getParameter("a");
htmlurl = java.net.URLDecoder.decode(htmlurl,"UTF-8");


最後說明 encodeURIComponent
主要是會針對特殊符號處理用

W3C 說明:http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp
該方法不會對ASCII 字母和數字進行編碼,也不會對這些ASCII 標點符號進行編碼: - _ . ! ~ * ' ( ) 。


其他字符(比如:;/?:@&=+$,# 這些用於分隔URI 組件的標點符號),都是由一個或多個十六進制的轉義序列替換的。

詳細英文說明:
https://blogs.msdn.microsoft.com/yangxind/2006/11/08/dont-use-net-system-uri-unescapedatastring-in-url-decoding/

2017年9月25日 星期一

【Entity Framework】IQueryable VS IEnumerable 用法 或 差別  (ASP NET, Entity Framework,C#)

IQueryable 每次直接撈 DB 資料庫 , 花費 DB 暫存記憶體

IEnumerable 第一次撈 DB 資料庫,花費 DB 暫存記憶體,以及暫存記體在(Web.IIS 程序)上
    再撈第二次,直接勞取「暫存記體在(Web.IIS 程序)上」,不再撈 DB
    除非 暫存記體在(Web.IIS 程序) 被 Entity Framework 指名 清掉,才會重新撈取

2017年8月16日 星期三

【瀏覽器】Chrome 瀏覽器 版本 記錄

Chrome 49  XP 最後版

Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36

圖示如下:





Chrome 18 穩定版

Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.3 Safari/535.19

圖示如下:




2017年6月19日 星期一

【Windows API】C# UNC 網路磁碟機 或 網路資料夾 mount 路磁碟機代號

Windows API
UNC 網路磁碟機 或 網路資料夾

今天的教學不使用 net command 指令

教程課程:
Netapi32.dll

其中三種 函式

1.NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, int level, ref USER_INFO_? buf, int parm_err)
說明:新增一個連線資訊
參數說明:
servername:DNS or NetBIOS name名稱,如果沒有用可以設 null

level:參數有4種 (1.2.3.4 分別USER_INFO_1 - USER_INFO_2 )就是指資料型態是哪種

buf:回傳 USER_INFO_?

parm_err:用於 NetUserSetInfo ,如果沒用可以設 null

2.NetUserDel([MarshalAs(UnmanagedType.LPWStr)] string servername,[MarshalAs(UnmanagedType.LPWStr)] string username);
說明:刪除現有連線資訊

3.NetUserGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername,[MarshalAs(UnmanagedType.LPWStr)] string username,int level,out IntPtr bufptr);
說明:查詢現有連結資訊,等同反相得到資料
servername:可以null
username:權限帳號
level:同 NetUserAdd
out bufptr:IntPtr 指標  會收到(結果)   -->Marshal.PtrToStructure(bufPtr, typeof(USER_INFO_?))
Marshal:Marshal.PtrToStructure從 Unmanaged 記憶體區塊封送處理資料到新配置的指定類型的 Managed 物件。

USER_INFO_2 (level 資料型態:2)其它1.3.4 都大同小異
MSDN 網址:https://msdn.microsoft.com/zh-tw/library/windows/desktop/aa371337(v=vs.85).aspx

參數:
LPWSTR usri2_name;
LPWSTR usri2_password; --->密碼
DWORD usri2_password_age;
DWORD usri2_priv;
LPWSTR usri2_home_dir;
LPWSTR usri2_comment;
DWORD usri2_flags;
LPWSTR usri2_script_path;
DWORD usri2_auth_flags;
LPWSTR usri2_full_name;
LPWSTR usri2_usr_comment;
LPWSTR usri2_parms;
LPWSTR usri2_workstations;
DWORD usri2_last_logon;
DWORD usri2_last_logoff;
DWORD usri2_acct_expires;
DWORD usri2_max_storage;
DWORD usri2_units_per_week;
PBYTE usri2_logon_hours;
DWORD usri2_bad_pw_count;
DWORD usri2_num_logons;
LPWSTR usri2_logon_server;
DWORD usri2_country_code;
DWORD usri2_code_page;


Msdn C++ 說明及範例:
網址:https://msdn.microsoft.com/zh-tw/library/windows/desktop/aa370654(v=vs.85).aspx

其它網站說明:
NetUserAdd
網址:http://www.pinvoke.net/default.aspx/netapi32/NetUserAdd.html
網址:http://www.securitypronews.com/using-the-network-functions-in-c-part-i-user-functions-2003-06
其它中文網址:http://www.cnblogs.com/tonyqus/archive/2004/12/26/82120.aspx
其它範例 網址:http://www.securitypronews.com/using-the-network-functions-in-c-part-i-user-functions-2003-06

NetUserGetInfo
其它範例 網址:http://www.pinvoke.net/default.aspx/netapi32/NetUserGetInfo.html

Marshal
MSDN網址:https://msdn.microsoft.com/zh-tw/library/system.runtime.interopservices.marshal(v=vs.110).aspx

Marshal.PtrToStructure
MSDN 網址:https://msdn.microsoft.com/zh-tw/library/4ca6d5z7(v=vs.110).aspx

2016年9月6日 星期二

【ASP NET.MVC, C# 】 Class to Json returun String to html 解析 String to obj

【ASP NET.MVC, C# 】Class to Json returun String to html 解析 String to obj

Action
return Json(save_result, "text/html", JsonRequestBehavior.AllowGet);

Html
JSON.parse(data.result);


2016年6月21日 星期二

【Asp.net C#】 byte 轉換 Base64String 、byte 內含中文處理方式


Convert.ToBase64String 方法 (Byte[])
https://msdn.microsoft.com/zh-tw/library/dhx0d524(v=vs.110).aspx

Convert.FromBase64String 方法 (String)
https://msdn.microsoft.com/zh-tw/library/system.convert.frombase64string(v=vs.110).aspx

ASCII只能處理單byte字元(ascii碼介於0~255)無法處理中文


字串以系統編碼方式轉換成Byte的形式
System.Text.Encoding.Default.GetBytes(String)

Function 參數:[Object-Byte陣列],[起值],[未端長度]
System.Text.Encoding.Default.GetString(temp_byte,0,temp.length)


byte 合併  function 參考國外
【方法1】
public static byte[] Combine(byte[] first, byte[] second)
{
    byte[] result= new byte[first.Length + second.Length];
    Buffer.BlockCopy(first, 0, ret, 0, first.Length);
    Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
    return result;
}

【方法2】
List temp_list = new List();
foreach (byte item in head_byte.ToList())
  temp_list.Add(item);

【方法3】
byte[] result  = head_check.Concat(head_byte).Concat(body_byte).Concat(end_byte).ToArray();

參考網址:
http://stackoverflow.com/questions/415291/best-way-to-combine-two-or-more-byte-arrays-in-c-sharp