好的,以下是使用 C# 实现的方案,同样遵循"获取EXE自身目录 → 拼接INI文件名"的核心思路,无需区分EXE在文件夹还是根目录。
核心方法
使用 Application.StartupPath 或 Assembly.GetExecutingAssembly().Location 获取EXE所在目录,然后与INI文件名拼接。
方案一:使用 Application.StartupPath(WinForms / WPF 推荐)
cs
using System;
using System.IO;
using System.Windows.Forms; // 需要添加System.Windows.Forms引用
public static class IniHelper
{
public static string GetIniFilePath(string iniFileName)
{
// Application.StartupPath 返回EXE所在目录(不含末尾反斜杠)
string exeDir = Application.StartupPath;
// 拼接INI文件完整路径
return Path.Combine(exeDir, iniFileName);
}
}
// 使用示例
class Program
{
static void Main()
{
string iniPath = IniHelper.GetIniFilePath("config.ini");
Console.WriteLine($"INI文件路径: {iniPath}");
// 输出示例:
// EXE在 C:\MyApp\app.exe → C:\MyApp\config.ini
// EXE在 D:\app.exe → D:\config.ini
}
}
方案二:使用 Assembly.GetExecutingAssembly()(控制台/类库通用)
cs
using System;
using System.IO;
using System.Reflection;
public static class IniHelper
{
public static string GetIniFilePath(string iniFileName)
{
// 获取当前EXE的完整路径
string exeFullPath = Assembly.GetExecutingAssembly().Location;
// 提取目录部分
string exeDir = Path.GetDirectoryName(exeFullPath);
// 拼接INI文件完整路径
return Path.Combine(exeDir, iniFileName);
}
}
// 使用示例
class Program
{
static void Main()
{
string iniPath = IniHelper.GetIniFilePath("config.ini");
Console.WriteLine($"INI文件路径: {iniPath}");
// 输出示例同上
}
}
方案三:使用 Environment.ProcessPath(.NET 6+ 推荐)
cs
using System;
using System.IO;
public static class IniHelper
{
public static string GetIniFilePath(string iniFileName)
{
// .NET 6+ 提供的简洁API
string exePath = Environment.ProcessPath;
string exeDir = Path.GetDirectoryName(exePath);
return Path.Combine(exeDir, iniFileName);
}
}
三种方案的对比
| 方案 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
Application.StartupPath |
WinForms / WPF | 最简洁,自动处理路径格式 | 需要引用 System.Windows.Forms |
Assembly.GetExecutingAssembly() |
所有.NET项目 | 纯.NET标准,不依赖UI框架 | 稍多一行代码 |
Environment.ProcessPath |
.NET 6+ | 官方推荐,现代简洁 | 不支持旧版.NET Framework |
为什么不建议用 Directory.GetCurrentDirectory()
cs
// ❌ 错误做法
string iniPath = Path.Combine(Directory.GetCurrentDirectory(), "config.ini");
原因:
-
当前工作目录可以被外部修改(如通过快捷方式启动时指定了不同的起始位置)
-
无法保证始终等于EXE所在目录
完整的INI读写示例(使用kernel32.dll)
如果需要实际读写INI文件内容,可以借助 Windows API:
cs
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
public class IniFile
{
private string filePath;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(
string section, string key, string def,
StringBuilder retVal, int size, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern bool WritePrivateProfileString(
string section, string key, string value, string filePath);
public IniFile(string iniFileName)
{
// 关键:使用EXE目录拼接路径
string exeDir = AppDomain.CurrentDomain.BaseDirectory;
this.filePath = Path.Combine(exeDir, iniFileName);
}
public string Read(string section, string key, string defaultValue = "")
{
var sb = new StringBuilder(255);
GetPrivateProfileString(section, key, defaultValue, sb, 255, filePath);
return sb.ToString();
}
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, filePath);
}
}
// 使用示例
class Program
{
static void Main()
{
var config = new IniFile("settings.ini");
// 写入
config.Write("Database", "Server", "localhost");
config.Write("Database", "Port", "3306");
// 读取
string server = config.Read("Database", "Server");
Console.WriteLine($"服务器地址: {server}");
}
}
总结
无论EXE在文件夹内还是盘符根目录,C#中统一使用
Application.StartupPath、Assembly.GetExecutingAssembly().Location或Environment.ProcessPath获取EXE所在目录,再用Path.Combine拼接INI文件名即可。无需编写任何条件分支来判断目录层级。
提示词如下。
使用ini文件存储配置信息,在读取ini文件在EXE所在同级目录时,如何区分处理EXE在文件夹内和EXE在盘符根目录
使用C#实现。
特此记录
anlog
2026.8.30