現在Unityでゲームを作成しています。
あるディレクトリにあるファイル名を取得するコードを以下のように書いていました。
c#
1public static class ClassLoader 2 { 3 public static string[] GetClassNames(string path) 4 { 5 var files = Directory.GetFiles(path); 6 var classNames = files 7 .Where(f => f.EndsWith(".cs")) 8 .Select(f => Path.GetFileNameWithoutExtension(f)) 9 .ToArray(); 10 return classNames; 11 } 12 }
引数にはC:\project_name\Assets\Scripts\sample\sample2\sample3
のようにフルパスを与えていました。
ディレクトリ構成は
├── sample ├── sample2 ├── sample3 │ ├── GetClassNamesを呼び出すクラス.cs
のようになっていて、変わる恐れのある(sample2以下の構成は不変の予定)フルパスよりも、相対パスで書きたいと思い
string CurrentDir = Directory.GetCurrentDirectory(); string targetDir = CurrentDir + "\sample3";
のようなコードを用意しましたが、
GetCurrentDirectory()
の結果がプロジェクトのルートC:\project_name
になり思うような動作になりませんでした。
調べてみたところ、GetCurrentDirectory()
は実行ファイル(.exe)などのディレクトリしか取得できないようでした。
関数実行時のパスを取得する方法はないのでしょうか?
そもそも相対パスでやろうとしているのが間違いなのでしょうか?
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー