發表文章

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

C# 來取得365 Outlook相關資訊,使用 Microsoft.Graph 讀取 帳號、帳號管理者、行事曆、行事曆事件等資訊

圖片
 在學習此操作之前先準備了解一下相關資訊及學習資源 線上API範本 https://developer.microsoft.com/en-us/graph/graph-explorer 線上操作文件 https://learn.microsoft.com/en-us/graph/overview?view=graph-rest-1.0 https://learn.microsoft.com/zh-cn/graph/overview 基本上我們必須先準備好下列4個數據資訊 string clientId = "應用程式 (用戶端) 識別碼" ; string authority = "目錄 (租用戶) 識別碼" ; string account = "Office 365 使用者的電子郵件信箱" ; string password = "Office 365 使用者的密碼" ; clientId = "應用程式 (用戶端) 識別碼" authority = "目錄 (租用戶) 識別碼" 操作權限的開啟 權限開放 /// <summary> /// 取得授權 /// </summary> /// <returns></returns> static async Task<AuthenticationResult> GetAuth() { string[] scopes = new string[] { "user.read", "Calendars.ReadBasic" }; IPublicClientApplication app; app = PublicClientApplicationBuilder.Create(clientId) .WithAuthority($"https://login.microsoftonline.com/{authority}") .Build(); AuthenticationResult result = null; try { // 使用...

使用for迴圈循環Dictionary

Dictionary<string, string> D = new Dictionary<string, string>();  D.add(key,value);  for(int i = 0; i< D.count ; i ++)  {      KeyValuePair<string, string> KVP = D.ElementAt(i);  }

DataTable去除沒有值得Column

如果要去除Datatable中沒有值得欄位~ 一般最直覺的做法就是使用迴圈爬所有的 Row來判斷出該欄位是否有值,但如果資料量過大的時候效能一定會有很大的問題,使用Datatable中的Compute就能很簡單的解決此問題 int minLavel = Convert . ToInt32 ( dt . Compute ( "min(AccountLevel)" , string . Empty ));

DataTable convert to List

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; namespace ifabTableToList {     public class ifabTableToList     {         public static List<T> Convert<T>(DataTable dt) where T : class         {             try             {                 if (dt == null || dt.Rows.Count == 0)                     return null;                 T cls = Activator.CreateInstance(typeof(T)) as T;                 var proInfo = cls.GetType().GetProperties();                 List <T> lsObj = new List<T>();                 for (int i = 0; i < dt.Ro...

如何讓WinForm只能同時開啟一個

於 Program.cs 中修改以下程式內容 static class Program    {        /// <summary>        /// The main entry point for the application.        /// C# stathread vs mtathread - please use google search        /// </summary>        [STAThread]        static void Main()        {            //防止應用程式開啟兩次,以下為程式碼            //參考網址            //http://topic.csdn.net/u/20100122/12/f395050d-06b7-4b29-8a7d-fb243d079c81.html            //http://msdn.microsoft.com/zh-tw/library/01985e8f%28v=vs.80%29            //sample start            //宣告變數,用來判斷是否重複開啟應用程式            bool isNotDuplicateApplication = false;       ...

C# string和byte[]的轉換

圖片
string類型轉成byte[]: byte [] byteArray  =  System.Text.Encoding.Default.GetBytes ( str ); 反過來,byte[]轉成string: string  str  =  System.Text.Encoding.Default.GetString ( byteArray ); 其它編碼方式的,如System.Text.UTF8Encoding,System.Text.UnicodeEncoding class等;例如: string類型轉成ASCII byte[]:("01" 轉成 byte[] = new byte[]{ 0x30, 0x31}) byte [] byteArray  =  System.Text.Encoding.ASCII.GetBytes ( str ); ASCII byte[] 轉成string:(byte[] = new byte[]{ 0x30, 0x31} 轉成 "01") string  str  =  System.Text.Encoding.ASCII.GetString ( byteArray ); 有時候還有這樣一些需求: byte[] 轉成原16進制格式的string,例如0xae00cf, 轉換成 "ae00cf";new byte[]{ 0x30, 0x31}轉成"3031":          public   static   string  ToHexString (  byte [] bytes )  //  0xae00cf => "AE00CF "          {             string hexString = string.Empty;      ...