「C Sharp」の版間の差分
ナビゲーションに移動
検索に移動
227行目: | 227行目: | ||
list = list.OrderBy(m => Guid.NewGuid()).ToList(); | list = list.OrderBy(m => Guid.NewGuid()).ToList(); | ||
</pre> | </pre> | ||
+ | |||
+ | ===ファイルを読んで書く=== | ||
+ | <pre> | ||
+ | var inPath = @"..."; | ||
+ | var outPath = @"..."; | ||
+ | |||
+ | using (var writer = File.CreateText(outPath)) | ||
+ | { | ||
+ | var lines = File.ReadAllLines(inPath).ToList(); | ||
+ | lines.ForEach(line => { | ||
+ | writer.WriteLine(line); | ||
+ | }); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | |||
[[category:プログラミング言語]] | [[category:プログラミング言語]] |
2023年7月26日 (水) 02:11時点における版
| Visual Studio | VC++ | Visual Studio Code |
目次
C#
言語まとめ C#
概要
Win32 API DLL の利用
- C# Win32 API および DLL の利用
- .NET6 の Windows Formsから user32.dllの SetWindowsHookEx を呼び出すが ERROR_MOD_NOT_FOUND(126) エラーになる対処
Windows Forms
C# Windows Forms Tips
C# 設定情報を保存する
データベース
SQL Server Compact
制御
書式
Sleep
画面・コントロール
グラフ
タスクトレイ
リソース
文字列
デバッグ
Visual Studio
Visual Studio 2010 Express C#
コーディング規約
Subversion プラグイン
Tips
日付のパース
- public static readonly string DATETIME_FORMAT_ISO8601 = "yyyy-MM-ddTHH:mm:ss.fffZ";
- DateTime.ParseExact(
- param.value,
- DateTimeUtil.DATETIME_FORMAT_ISO8601,
- System.Globalization.CultureInfo.InvariantCulture));
属性の指定と読み込み
- 属性定義
- [System.AttributeUsage(System.AttributeTargets.Property)]
- public class PrimaryKeyAttribute : System.Attribute
- {
- }
- 読み出し(Hogeクラスのプロパティに属性をつけているとする)
- var type = typeof(Hoge);
- var properties = type.GetProperties();
- foreach(var property in properties)
- {
- foreach(var attr in property.GetCustomAttributes(true))
- {
- Console.WriteLine($"[{attr}]");
- }
- Console.WriteLine($"{property.Name}");
- }
Docコメント
https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/xmldoc/recommended-tags
- 複数の要素に使用される 一般的なタグ - これらのタグは、API 用の最小セットです
- summary: この要素の値は、Visual Studio の IntelliSense に表示されます。
- remarks: **
- メンバーに使用されるタグ - これらのタグは、メソッドおよびプロパティを文書化する場合に使用されます。
- returns: この要素の値は、Visual Studio の IntelliSense に表示されます。
- param: *: この要素の値は、Visual Studio の IntelliSense に表示されます。
- paramref:
- exception: *
- value:: この要素の値は、Visual Studio の IntelliSense に表示されます。
- ドキュメント出力の書式設定 - これらのタグを使用して、ドキュメントを生成するツールに書式設定を指示します。
- para:
- list:
- c:
- code:
- example: **
- ドキュメント テキストの再使用 - これらのタグを使用すると、ツールで XML コメントを再使用しやすくなります。
- inheritdoc: **
- include: *
- リンクと参照の生成 - これらのタグを使用して、他のドキュメントへのリンクを生成します。
- see: *
- seealso: *
- cref:
- href:
- ジェネリック型およびメソッド用のタグ - これらのタグは、ジェネリック型およびメソッドでのみ使用されます。
- <typeparam> *: この要素の値は、Visual Studio の IntelliSense に表示されます。
- <typeparamref>
UIスレッド
C Sharp 非同期処理からUIスレッドにアクセスし画面を更新する
スクレイピング
- using AngleSharp.Html.Parser;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- namespace Hoge
- {
- public class Crawler
- {
- private static HttpClient _client = new HttpClient();
- public async void Fetch(string url)
- {
- using(var stream = await _client.GetStreamAsync(url))
- {
- var parser = new HtmlParser();
- var doc = parser.ParseDocument(stream);
- var ankers = doc.All.Where(m => m.LocalName == "a");
- foreach(var anker in ankers)
- {
- Debug.WriteLine(anker.TextContent);
- }
- }
- }
- }
- }
HttpClient Query String
- var param = (new FormUrlEncodedContent(dict)).ReadAsStringAsync().GetAwaiter().GetResult();
文字列配列を結合してCSVを作成
- string[] row = 文字列配列
- return string.Join(GetDelimiters(), row.Select(f => $"\"{f}\"").ToArray());
- ¥{0¥} を生成する場合
- var i = 0;
- var fmt = @$"¥{{{i}¥}}";
文字コード変換
正規表現による置換
- var output = System.Text.RegularExpressions.Regex.Replace(input, @"^[ ]", "");
string を stream に変換
- return new MemoryStream(Encoding.UTF8.GetBytes(value ?? ""));
ディレクトリを再帰的に表示
- class Program
- {
- static void Main(string[] args)
- {
- var me = new Program();
- me.Parse(args[0], 0);
- }
- private void Parse(string path, int depth)
- {
- var indent = new string(' ', depth * 2);
- foreach (var entry in Directory.EnumerateFileSystemEntries(path))
- {
- var attr = File.GetAttributes(entry);
- if (attr.HasFlag(FileAttributes.Directory))
- {
- Console.WriteLine($"{indent}{Path.GetDirectoryName(entry)}\\");
- this.Parse(entry, depth + 1);
- }
- Console.WriteLine($"{indent}{Path.GetFileName(entry)}");
- }
- }
- }
SHIFT-JIS 文字列から、SO SI を除去
- var encShiftJis = Encoding.GetEncoding("shift_jis");
- int lino = 1;
- using (var reader = new StreamReader(path, encShiftJis))
- {
- string line = null;
- while ((line = reader.ReadLine())!=null)
- {
- byte[] bytes = encShiftJis.GetBytes(line);
- for(int i=0;i<bytes.Length; i++)
- {
- // bytes = bytes.Where(b => (b != 0x20 /*space*/ && b != 0x61 /*'a'*/)).ToArray();
- bytes = bytes.Where(b => (b != 0x0E /*SO*/ || b != 0x0F /*SI*/)).ToArray();
- }
- line = encShiftJis.GetString(bytes);
- Console.WriteLine($"{indent}{lino++:D4}:{line}");
- }
- }
リストのシャッフル
- list = list.OrderBy(m => Guid.NewGuid()).ToList();
ファイルを読んで書く
- var inPath = @"...";
- var outPath = @"...";
- using (var writer = File.CreateText(outPath))
- {
- var lines = File.ReadAllLines(inPath).ToList();
- lines.ForEach(line => {
- writer.WriteLine(line);
- });
- }
© 2006 矢木浩人