| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「C Sharp」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
102行目: 102行目:
 
===string を stream に変換===
 
===string を stream に変換===
 
   return new MemoryStream(Encoding.UTF8.GetBytes(value ?? ""));
 
   return new MemoryStream(Encoding.UTF8.GetBytes(value ?? ""));
===ディレクトリを再帰的に表===
+
===ディレクトリを再帰的に表示===
 
  class Program
 
  class Program
 
  {
 
  {

2021年11月29日 (月) 06:21時点における版

| Visual Studio | VC++ | Visual Studio Code |

C#

言語まとめ C#

概要

Win32 API DLL の利用

Windows Forms

C# Windows Forms Tips

C# 設定情報を保存する

データベース

SQL Server Compact

制御

書式

Sleep

画面・コントロール

グラフ

タスクトレイ

リソース

文字列

デバッグ

Visual Studio

Visual Studio 2010 Express C#

コーディング規約

Subversion プラグイン

Tips

UIスレッド

C Sharp 非同期処理からUIスレッドにアクセスし画面を更新する

スクレイピング

AngleSharp

  1. using AngleSharp.Html.Parser;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Text;
  9.  
  10. namespace Hoge
  11. {
  12. public class Crawler
  13. {
  14. private static HttpClient _client = new HttpClient();
  15. public async void Fetch(string url)
  16. {
  17. using(var stream = await _client.GetStreamAsync(url))
  18. {
  19. var parser = new HtmlParser();
  20. var doc = parser.ParseDocument(stream);
  21.  
  22. var ankers = doc.All.Where(m => m.LocalName == "a");
  23. foreach(var anker in ankers)
  24. {
  25. Debug.WriteLine(anker.TextContent);
  26. }
  27.  
  28. }
  29.  
  30. }
  31.  
  32. }
  33. }

文字列配列を結合してCSVを作成

  1. string[] row = 文字列配列
  2. return string.Join(GetDelimiters(), row.Select(f => $"\"{f}\"").ToArray());

文字コード変換

正規表現による置換

  1. var output = System.Text.RegularExpressions.Regex.Replace(input, @"^[ ]", "");

string を stream に変換

  1. return new MemoryStream(Encoding.UTF8.GetBytes(value ?? ""));

ディレクトリを再帰的に表示

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var me = new Program();
  6. me.Parse(args[0], 0);
  7. }
  8.  
  9. private void Parse(string path, int depth)
  10. {
  11. var indent = new string(' ', depth * 2);
  12. foreach (var entry in Directory.EnumerateFileSystemEntries(path))
  13. {
  14. var attr = File.GetAttributes(entry);
  15. if (attr.HasFlag(FileAttributes.Directory))
  16. {
  17. Console.WriteLine($"{indent}{Path.GetDirectoryName(entry)}\\");
  18. this.Parse(entry, depth + 1);
  19. }
  20. Console.WriteLine($"{indent}{Path.GetFileName(entry)}");
  21. }
  22. }
  23. }

SHIFT-JIS 文字列から、SO SI を除去

  1. var encShiftJis = Encoding.GetEncoding("shift_jis");
  2. int lino = 1;
  3. using (var reader = new StreamReader(path, encShiftJis))
  4. {
  5. string line = null;
  6. while ((line = reader.ReadLine())!=null)
  7. {
  8. byte[] bytes = encShiftJis.GetBytes(line);
  9. for(int i=0;i<bytes.Length; i++)
  10. {
  11. // bytes = bytes.Where(b => (b != 0x20 /*space*/ && b != 0x61 /*'a'*/)).ToArray();
  12. bytes = bytes.Where(b => (!= 0x0E /*SO*/ ||!= 0x0F /*SI*/)).ToArray();
  13. }
  14. line = encShiftJis.GetString(bytes);
  15. Console.WriteLine($"{indent}{lino++:D4}:{line}");
  16. }
  17. }