C#
作成日時:2018/11/28
更新日時:2019/06/26
メニュー
はじめにIntroduction | Microsoft Docs
拡張子取得
以下テストコードの変数extensionに.pngが格納されます。
using System.IO;
public class Sample {
void Test () {
var path = "/sample/path/test.png"
var extension = Path.GetExtension(path);
}
}
指定ディレクトリのファイル一覧取得
以下テストコードの変数filesに変数pathで指定したパスにあるファイルのリストが格納され、foreachループ等で確認等が行えます。
using System.IO;
public class Sample {
void Test () {
var path = "/sample/path"
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
}
}
}
参考リンク
https://docs.microsoft.com/ja-jp/dotnet/api/system.io.directory.getfiles?view=netframework-4.8
指定ディレクトリのサブディレクトリ一覧取得
以下テストコードの変数dirsに変数pathで指定したパスにあるサブディレクトリのリストが格納され、foreachループ等で確認等が行えます。
using System.IO;
public class Sample {
void Test () {
var path = "."
string[] dirs = System.IO.Directory.GetDirectories(path);
foreach (string dir in dirs)
{
}
}
}
参考リンク
https://docs.microsoft.com/ja-jp/dotnet/api/system.io.directory.getdirectories?view=netframework-4.8
リストのランダムシャッフル
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
public class Sample {
void Test () {
public List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list = list.OrderBy(a => Guid.NewGuid()).ToList();
list.ForEach(delegate (int num)
{
});
}
}
string型の配列をstring型に変換
区切り文字に改行コードを指定
string str = string.Join("\n", str_array);
文字列の先頭部分空白を削除
以下コードにて、変数str_afterには先頭の空白部分が削除されたテキストが格納されます。
string str = " 先頭に空白を含むテキスト";
string str_after = str.TrimStart(' ');
https://docs.microsoft.com/ja-jp/dotnet/api/system.string.trimstart?view=netframework-4.8
パス文字列からファイル名取得
string path = "/usr/name/test/test.cs // test.cs string fileName = Path.GetFileName(path); // .cs string fileExtension = Path.GetExtension(path);
エラー
CS1012: Too many characters in character literal
確認バージョン
Unity Version 2018.2.14f1 Personal
Visual Studio Community 2017 for Mac Version 7.6.10 (build 27)
修正
文字リテラルを'で囲っていると発生、"で囲うように修正。
Debug.Log('Application.persistentDataPath' + Application.persistentDataPath);
Debug.Log("Application.persistentDataPath" + Application.persistentDataPath);
表示
error CS1012: Too many characters in character literal
CS1612: Cannot modify the return value of
https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/compiler-messages/cs1612
using System.Collections.Generic;
public class Sample
{
struct STest
{
public int testParam;
}
void Test ()
{
STest sTest = new STest();
sTest.testParam = 1;
List<STest> cardPosList = new List<STest>();
cardPosList.Add(sTest);
for (int i = 0, max = cardPosList.Count; i < max; i++)
{
cardPosList[i].testParam = 2;
}
}
}
表示
Error CS1612: Cannot modify the return value of
Visual Studioで該当行にマウスオーバーした際には以下のような表示がされます。
変数ではないため、 'List<AnyTest.STest>.this[int]' の戻り値を変更できません
'IndexOf' はカルチャ対応で、StringComparison 引数がありません。
確認バージョン
Visual Studio Community 2017 for Mac Version 7.6.10 (build 27)
修正
StringComparison引数を追加
string test = "test";
int count = test.IndexOf("test");
string test = "test";
int count = test.IndexOf("test", System.StringComparison.CurrentCulture);
https://docs.microsoft.com/ja-jp/dotnet/api/system.string.indexof?view=netframework-4.8
https://docs.microsoft.com/ja-jp/dotnet/api/system.stringcomparison?view=netframework-4.8