LoginSignup
1
2

More than 5 years have passed since last update.

実行ファイルと同じディレクトリにあるファイルへアクセスする

Posted at

はじめに

実行ファイルと同じディレクトリにある設定ファイルなどへアクセスしたいことがあります。
そのようなときの良くある書き方というものをまとめていきたいと思います。

もっと効率の良い方法があるとか、他の言語などの情報を、お待ちしています。

環境

Windows 7以降を対象とします。

C/C++

getModulePath.c
#include <windows.h>
#include <string.h>

#define BUF_SIZE 1024

TCHAR name[BUF_SIZE];
TCHAR* lastdelim;
GetModuleFileName(NULL, name, BUF_SIZE);
lastdelim = _tcsrchr(name, '\\');
*lastdelim = '\0';

C# / .NET

getModulePath.cs
using System;
using System.Reflection;
using System.IO;

Assembly assem = Assembly.GetExecutingAssembly();
String path = Path.GetDirectoryName(assem.Location);

python

getModulePath.py
os.path.dirname(os.path.abspath(__file__))
1
2
1

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
1
2