Brian的雜記
  • Introduction
  • Brian's 雜記
    • My Awesome API
    • FB 大頭貼
    • 硬體雜記
    • PHP
    • project
      • 模擬器
      • WAMP
    • WinMerge
    • 雜記
      • LINQ
      • 方法
      • Grid View
      • namespace
      • global
      • 物件導向
      • Excel
      • VS2017
      • single sign on
      • Master
      • Https
      • 憑證
      • 略過憑證不符
      • NLog
      • 團隊開發
      • .NET Core
      • 共用網路上的芳鄰
      • 爬蟲
      • NPOI
      • RSS
      • 多執行緒
      • 記憶體回收
      • 密碼學
        • BCrypt
        • AES
      • 主機環境建置
      • Session
      • Error
      • IIS 相關
      • 無障礙相關
      • 介面
        • 影像地圖
      • telnet
        • smtp
      • nslookup
      • 協助客戶解決問題
      • 驗證欄位
      • 網站管理
      • 工具整理
    • 正規表示法
    • 影像處理
    • IntelliJ Idea
    • 觀念
      • Clean Code
        • 命名
        • 函式
        • 註解
        • 編排
        • 物件及資料結構
        • 錯誤處理
        • 邊界
        • 單元測試
        • 類別
      • Java 程式風格
      • Design Pattern
        • 單例模式
      • 同步
      • 畫圖
        • ER-Model
        • 類別圖
        • Use Case
        • 有限狀態機
      • 資料 API 文件 分析
      • CORS & SSL
      • 利用DISC幫助溝通
      • OAS
    • 檔案上傳
      • FileStore
      • App Engine
      • Google Storage
    • OAuth vs Open ID
    • MIME
    • 虛擬桌面
    • 待看資料
    • Selenium
    • CDN
    • HTTP
    • 編碼
    • 2nd-ML100Days
      • jupyter
    • 微服務
      • 設計
        • 1 ~ 5
        • 6
        • 7
        • 8
        • 9
    • Gradle
    • Maven
    • Error
    • 批次檔 BAT
    • Kurento
    • WebSocket & WebRTC
  • 需求面能力
    • User Story
  • Google Cloud Platform
    • Compute Engine
  • Python
    • 基本語法
    • Pandas
    • 套件
    • Matplotlib
    • Encoder
    • jupyter
  • Java
    • Java
      • File
      • Exception
      • 物件導向觀念
      • 加密
      • HTTP
      • 集合
      • Stream()
      • Web
      • ResultSet
      • JDK6
      • JDK8
    • 讀取、複寫MP3 Tag
    • Log4j2
    • Servlet
      • 容器
    • JSP
    • JBOSS
    • JWT
    • PreparedStatement
    • Error
    • Spring
      • Spring Boot
        • @Value
        • Build
      • RequestParameter
      • Error
      • Autowired
      • JPA
      • FeignClient
      • WebSocket
      • thymeleaf
      • Security
      • Test
      • Scheduled
      • Redirect
    • IntelliJ
  • Linux
    • Linux
    • Shell Script
    • Cygwin(在Windows執行Linux指令)
  • 前端
    • HTML
      • Link
    • CSS
      • Position
      • padding color
      • display
    • JS
      • jQuery
        • Select2
      • fancybox
      • ES6
      • 效能
      • GoogleMap API
        • Marker
        • InfoWindow
      • 事件
      • CKEditor
      • TGOS
      • JSON
      • QRcode
      • 核心概念
        • 物件 變數 型別
          • number
          • String
          • boolean
          • null & undefined
          • Symbol
        • JS 物件概念
        • 深入理解JS 函式物件
        • 更多ES2015/ES6 全新語言特性
      • Promise
    • 效果應用
  • 資料庫
    • 注意事項
    • MariaDB
    • MySQL
      • inner join 和 join
      • 字串比對
      • 倒數資料
    • SQL
      • DDL 資料定義語言
      • DML 資料操縱語言
      • DCL 資料控制語言
      • TCL 交易控制語言
      • T-SQL
      • CTE
      • JOIN
    • Oracle
    • MSSQL 操作
      • 新增使用者
      • SQL 指令
      • Sequence
    • 差異比較
    • MyBatis
    • Workbench 操作
    • SQL Injection
  • 版本控制
    • Gitlab
      • sign up
      • sign in
      • add project
      • add members
    • SourceGit
      • install
    • SmartGit
      • install
      • operate
      • git 操作雜記
    • TortoiseGit
    • Git
    • TFS
    • SVN
  • Test
    • 軟體測試原則
    • 演算法
    • XMind
      • install
    • Jenkins
      • 建置
    • HTTPie
    • Postman
    • 測試驅動開發
    • 撰寫測試的觀念
    • 測試框架
    • IoC & DI
    • 隔絕相依性的方式
    • JUnit
    • NUnit
    • 習慣
    • 虛設常式
  • Angular
    • hello world
    • ng-options
    • ES6
    • Build & Conponect
    • HttpClient
    • 部署
  • ASP.NET Web Form
    • Chapter 2
      • 2-1
        • 小東西
    • 略過請求驗證
  • Go
Powered by GitBook
On this page
  • 取得檔案名稱
  • 抓取網頁上的資料
  • List 的方法
  • 字串切割
  • URL解析
  • 判斷物件是否為null
  • 判斷字串為空或者null
  • 開執行緒去呼叫外部程式
  • DataTable
  • 產生隨機字串
  • 時間差、兩個DateTime之間的差異
  • 抓取LocalIP
  • 重新導向
  • 將後端資料丟給前端JavaScript
  • out ref
  • 為控制項增加HTML 屬性
  • Console.ReadLine 相關

Was this helpful?

  1. Brian's 雜記
  2. 雜記

方法

取得檔案名稱

GetFileNameWithoutExtension(filename)); //取得副檔名以前的部分
GetExtension(filename)); //取得副檔名
Path.GetPathRoot(filename)); //取得根目錄
Path.GetFullPath(filename)); //取得路徑

抓取網頁上的資料

WebClient wc = new WebClient(); //

        MemoryStream ms = new MemoryStream(wc.DownloadData(RSS_Source.Address));
        XDocument doc = XDocument.Load(ms);
        var query = (from feed in doc.Descendants("item")
                     select new
                     {
                         Title = feed.Element("title").Value,
                         Description = feed.Element("description").Value,
                         Date = DateTime.Parse(feed.Element("pubDate").Value),
                         link = feed.Element("link").Value,
                         testImage = feed.Elements("image").ToArray()
                     }).ToArray();

List 的方法

增加項目

void ListObject.Add(item);

檢查是否包含在List裡面

boolean ListObject.Contains(item);

直接創建一個新的相同內容的List物件

List<T> NewList = new ListObject.ToList();

字串切割

string str = "hello[myfriend]";
int indexS = 0;
int indexE = 0;
indexS = str.IndexOf("[");
indexE = str.IndexOf("]");
Console.WriteLine(str.Substring(indexS+1,indexE-indexS-1));
//myfriend

假如字串都有規律

str = str.Replace("hello[","").Replace("]","");

也可以得到相同效果

URL解析

判斷物件是否為null

boolean Object.Equals(obj1,obj2);

obj2 放 null obj1 放想要測試的物件即可

判斷字串為空或者null

boolean string.IsNullOrEmpty(str)

開執行緒去呼叫外部程式

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "檔案路徑";
p.StartInfo.Arguments = 參數1 + " " + 參數2;
p.StartInfo.UseShellExecute = false;
p.Start();

DataTable

產生隨機字串

string RandomPasswordString = System.Web.Security.Membership.GeneratePassword(字串長度,特殊字元數量);

時間差、兩個DateTime之間的差異

有Total 的 回傳值為double 沒有的 回傳值為 int 並無條件捨去

TimeSpan ts = datetime1 - datetime2;
ts.days //天數 無條件捨去
ts.Hours //小時 為 (總數 % 86400) / 3600
ts.Minutes //分鐘 為 ((總數 % 86400) % 3600) / 60
ts.TotalMinutes //總分鐘 總數 / 60

抓取LocalIP

using System.Net;

// 取得本機名稱
string strHostName = Dns.GetHostName();
// 取得本機的IpHostEntry類別實體,用這個會提示已過時
//IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

// 取得本機的IpHostEntry類別實體,MSDN建議新的用法
IPHostEntry iphostentry = Dns.GetHostEntry(strHostName);

// 取得所有 IP 位址
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
    // 只取得IP V4的Address
    if (ipaddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    {
        Console.WriteLine("Local IP: " + ipaddress.ToString());
    }
}

重新導向

Response.Redirect("http://www.microsoft.com/gohere/look.htm");

將後端資料丟給前端JavaScript

KeyWord : HiddenField

拿來當作資料儲存區,後端撈取完資料,整理成JSON格式,丟進去HiddenField,再讓前端用jQuery去取出來用。

可以去參考:Brian's雜記>雜記>前端>JSON

out ref

out & ref 都要先指派好變數,然後再丟去該位置接值。

差異

out 傳進去前不用初始化,但是方法內要初始化。

ref 傳進去前需要初始化,但方法內隨便用。

為控制項增加HTML 屬性

EX:點擊清空

txt1.Attributes.Add("onFocus","this.value=''");

Console.ReadLine 相關

Console.ReadLine 只能讀取長度 254 (256 - 兩個字元)

倘若一行超過這個長度,有兩種方法:

1.開一個新的輸入流

Stream inputStream = Console.OpenStandardInput(5000); 

Console.SetIn(new StreamReader(inputStream)); 

string body = Console.ReadLine();

2.自己寫一個ReadLine 出來

成品大致上長這樣

public static string ReadLineMoreLength(int size)
{
      Console.InputEncoding = Encoding.Unicode;
      Console.OutputEncoding = Encoding.Unicode;

      Stream inputStream = Console.OpenStandardInput(size);
      byte[] bytes = new byte[size];
      int outputLength = inputStream.Read(bytes, 0, size);
      //Console.WriteLine(outputLength);
      char[] chars = Encoding.Unicode.GetChars(bytes, 0, outputLength);
      return new string(chars);
}

我當初參考的範例中,並沒有上面那兩個編碼設定,底下的GetChars部分如下

char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength); //原先範例

char[] chars = Encoding.Unicode.GetChars(bytes, 0, outputLength); //Brian 改寫

當時會導致字串中的中文變成亂碼,後來參考了一些資料後,改寫成上面那樣,就正常了。

同場加映:

字串編碼問題、比對

PreviousLINQNextGrid View

Last updated 5 years ago

Was this helpful?

參考資料:ASP.NET 如何取得 Request URL 的各個部分()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

參考資料:()

https://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequest-object.aspx
https://msdn.microsoft.com/zh-tw/library/w4hkze5k(v=vs.110).aspx
https://dotblogs.com.tw/chjackiekimo/2014/04/03/144606
https://docs.microsoft.com/zh-tw/dotnet/framework/data/adonet/dataset-datatable-dataview/adding-data-to-a-datatable
http://limitedcode.blogspot.tw/2014/06/c_14.html
https://dotblogs.com.tw/skyline0217/2011/04/21/23269
https://blog.miniasp.com/post/2008/01/22/Find-the-difference-between-two-DateTime.aspx
https://dotblogs.com.tw/mingstyle/2013/03/09/96041
https://msdn.microsoft.com/zh-tw/library/540y83hx(v=vs.100).aspx
https://dotblogs.com.tw/erictsaiblog/2015/05/10/151238
https://docs.microsoft.com/zh-tw/dotnet/csharp/programming-guide/arrays/passing-arrays-using-ref-and-out
https://dotblogs.com.tw/mis0800/2014/02/17/144025
https://blog.csdn.net/zfrong/article/details/5457600
https://docs.microsoft.com/en-us/dotnet/api/system.console.openstandardinput?redirectedfrom=MSDN&view=netframework-4.7.2#overloads
https://stackoverflow.com/questions/9502488/reading-unicode-from-console/9555255
https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=netframework-4.7.2
https://dotblogs.com.tw/yc421206/archive/2011/06/09/27596.aspx