ini文件读取,EXE同目录文件读取

好的,以下是使用 C# 实现的方案,同样遵循"获取EXE自身目录 → 拼接INI文件名"的核心思路,无需区分EXE在文件夹还是根目录。


核心方法

使用 Application.StartupPathAssembly.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.StartupPathAssembly.GetExecutingAssembly().LocationEnvironment.ProcessPath 获取EXE所在目录,再用 Path.Combine 拼接INI文件名即可。

无需编写任何条件分支来判断目录层级。

提示词如下。

使用ini文件存储配置信息,在读取ini文件在EXE所在同级目录时,如何区分处理EXE在文件夹内和EXE在盘符根目录

使用C#实现。

特此记录

anlog

2026.8.30

相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹2 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
kybs19912 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
Polevne2 天前
C# SFTP客户端实现文件传输
c#·ssh
samble2 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
cjp5602 天前
024 . WPF MVVM类封装优化
c#
淡海水3 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
asdzx673 天前
如何使用 C# 提取 PPT 文本与图片
c#·ppt·提取文本
2501_930707783 天前
使用C#代码使用条件格式为 Excel 隔行设置颜色
c#·excel
何以解忧唯有撸码3 天前
【开源】c#造个轮子-日程安排工具
c#·自定义控件