You may want to access configuration files in the same directory as the executable file. I would like to summarize the common writing styles in such cases.
We are waiting for information on more efficient methods, other languages, etc.
For Windows 7 and above.
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__))
Recommended Posts