ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

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

ini文件读取,EXE同目录文件读取 好的以下是使用C#实现的方案同样遵循“获取EXE自身目录 → 拼接INI文件名”的核心思路无需区分EXE在文件夹还是根目录。核心方法使用Application.StartupPath或Assembly.GetExecutingAssembly().Location获取EXE所在目录然后与INI文件名拼接。方案一使用Application.StartupPathWinForms / WPF 推荐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()控制台/类库通用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 推荐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.StartupPathWinForms / WPF最简洁自动处理路径格式需要引用System.Windows.FormsAssembly.GetExecutingAssembly()所有.NET项目纯.NET标准不依赖UI框架稍多一行代码Environment.ProcessPath.NET 6官方推荐现代简洁不支持旧版.NET Framework为什么不建议用Directory.GetCurrentDirectory()// ❌ 错误做法 string iniPath Path.Combine(Directory.GetCurrentDirectory(), config.ini);原因当前工作目录可以被外部修改如通过快捷方式启动时指定了不同的起始位置无法保证始终等于EXE所在目录完整的INI读写示例使用kernel32.dll如果需要实际读写INI文件内容可以借助 Windows APIusing 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#实现。特此记录anlog2026.8.30
返回列表