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

相关推荐
rockey62744 分钟前
AScript之编译递归函数
c#·.net·script·动态脚本
yue0087 小时前
winform控件篇-按钮
c#
DevOpenClub11 小时前
Markdown、HTML 和 PPT 如何稳定交付:文档转换任务的幂等发布流程
开发语言·前端·c#·html·powerpoint
软件黑马王子12 小时前
3.单例模式问题1:构造函数
开发语言·单例模式·c#
软件黑马王子12 小时前
4.单例模式问题2:重复挂载
开发语言·单例模式·c#
夏霞14 小时前
c# 不支持的目标框架 解决方案
开发语言·c#
软件黑马王子14 小时前
5.单例模式问题3:多线程问题
单例模式·前端框架·c#
geovindu14 小时前
CSharp: Template Method Pattern
开发语言·后端·c#·.net·模板方法模式·行为模式
CSDN_RTKLIB14 小时前
SetImpliedSelection
c#