LoginSignup
7
5

More than 3 years have passed since last update.

C#でカレントディレクトリを取得する

Posted at

目的

  • C#でアプリケーションのカレントディレクトリを取得するメソッドの挙動検証

取得方法いろいろ

System.AppDomain.CurrentDomain.BaseDirectory

Directory.GetCurrentDirectory()

Directory.GetParent(Assembly.GetExecutingAssembly().Location)

Environment.CurrentDirectory

System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(Environment.GetCommandLineArgs()[0]))

System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)

検証

検証用のソースコード

pwd
  # /home/ec2-user/dotnet-app
ls -l
  # total 8
  # -rw-rw-r-- 1 ec2-user ec2-user 225 Aug 15 03:47 dotnet-app.csproj
  # -rw-rw-r-- 1 ec2-user ec2-user 751 Aug 15 04:03 Program.cs
Program.cs
using System;
using System.IO;
using System.Reflection;

namespace dotnet_app
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
            Console.WriteLine(Directory.GetCurrentDirectory());
            Console.WriteLine(Directory.GetParent(Assembly.GetExecutingAssembly().Location));
            Console.WriteLine(Environment.CurrentDirectory);
            Console.WriteLine(System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(Environment.GetCommandLineArgs()[0])));
            Console.WriteLine(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
        }
    }
}

プロジェクトファイル(.csproj)のあるディレクトリで dotnet run を実行

pwd
  # /home/ec2-user/dotnet-app
dotnet run
  # /home/ec2-user/dotnet-app/bin/Debug/netcoreapp3.1/
  # /home/ec2-user/dotnet-app
  # /home/ec2-user/dotnet-app/bin/Debug/netcoreapp3.1
  # /home/ec2-user/dotnet-app
  # /home/ec2-user/dotnet-app/bin/Debug/netcoreapp3.1
  # /home/ec2-user/dotnet-app/bin/Debug/netcoreapp3.1

プロジェクトファイル(.csproj)のあるディレクトリ以外で dotnet run を実行

pwd
  # /home/ec2-user
dotnet run --project dotnet-app/dotnet-app.csproj
  # /home/ec2-user/dotnet-app/bin/Debug/netcoreapp3.1/
  # /home/ec2-user
  # /home/ec2-user/dotnet-app/bin/Debug/netcoreapp3.1
  # /home/ec2-user
  # /home/ec2-user/dotnet-app/bin/Debug/netcoreapp3.1
  # /home/ec2-user/dotnet-app/bin/Debug/netcoreapp3.1

実行ファイルを作成

pwd
  # /home/ec2-user/dotnet-app
dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true
  # Microsoft (R) Build Engine version 16.7.0-preview-20360-03+188921e2f for .NET
  # Copyright (C) Microsoft Corporation. All rights reserved.
  # 
  #   Determining projects to restore...
  #   Restored /home/ec2-user/dotnet-app/dotnet-app.csproj (in 340 ms).
  #   dotnet-app -> /home/ec2-user/dotnet-app/bin/Release/netcoreapp3.1/linux-x64/dotnet-app.dll
  #   dotnet-app -> /home/ec2-user/dotnet-app/bin/Release/netcoreapp3.1/linux-x64/publish/

(実行ファイルのあるディレクトリで)実行ファイルを起動

pwd
  # /home/ec2-user/dotnet-app/bin/Release/netcoreapp3.1/linux-x64/publish
./dotnet-app
  # /var/tmp/.net/ec2-user/dotnet-app/pbhxfno5.xnt/
  # /home/ec2-user/dotnet-app/bin/Release/netcoreapp3.1/linux-x64/publish
  # /var/tmp/.net/ec2-user/dotnet-app/pbhxfno5.xnt
  # /home/ec2-user/dotnet-app/bin/Release/netcoreapp3.1/linux-x64/publish
  # /var/tmp/.net/ec2-user/dotnet-app/pbhxfno5.xnt
  # /home/ec2-user/dotnet-app/bin/Release/netcoreapp3.1/linux-x64/publish

実行ファイルを起動

pwd
  # /home/ec2-user/dotnet-app
./bin/Release/netcoreapp3.1/linux-x64/publish/dotnet-app
  # /var/tmp/.net/ec2-user/dotnet-app/pbhxfno5.xnt/
  # /home/ec2-user/dotnet-app
  # /var/tmp/.net/ec2-user/dotnet-app/pbhxfno5.xnt
  # /home/ec2-user/dotnet-app
  # /var/tmp/.net/ec2-user/dotnet-app/pbhxfno5.xnt
  # /home/ec2-user/dotnet-app/bin/Release/netcoreapp3.1/linux-x64/publish

考察

以下のような要件があった場合

  • 実行ファイルと同じディレクトリにファイルを出力させるプログラムを作成。
  • また、このプログラムは、他のアプリケーションから呼ばれることを想定している。

System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)) がいいのかな。

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5