日期:2014-05-19  浏览次数:20707 次

一个ini文件读取产生的问题
小弟在2005下写的程序,在执行路径下带一个ini文件,调试的时候一切正常,可以正常读取
但是我把程序在外部用双击执行的时候,读取ini文件就会出错

我读取ini路径时候是这样:
application.executepath.replace( "xxx.exe ", "ini.ini ");
外部执行就会出错。


但是当我把ini路进写成绝对路径的时候外部执行就可以正常读取配置,问各位是为什么?

------解决方案--------------------
Path.GetDirectoryName(Application.ExecutablePath) + "\\ini.ini "
------解决方案--------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;


namespace iniprocess
{

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;

[DllImport( "kernel32 ")]
private static extern long WritePrivateProfileString(string section,
string key,string val,string filePath);
[DllImport( "kernel32 ")]
private static extern int GetPrivateProfileString(string section,
string key,string def, StringBuilder retVal,
int size,string filePath);

public void IniWriteValue(string Section,string Key,string Value,string filepath)//对ini文件进行写操作的函数
{
WritePrivateProfileString(Section,Key,Value,filepath);
}

public string IniReadValue(string Section,string Key,string filepath)//对ini文件进行读操作的函数
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key, " ",temp,
255, filepath);
return temp.ToString();

}




private void button1_Click(object sender, System.EventArgs e)
{

this.textBox1 .Text= IniReadValue( "ODBC 32 bit Data Sources ", "MS Access Database ", "e:\\temp\\ODBC.INI ");

}

private void button2_Click(object sender, System.EventArgs e)
{

IniWriteValue ( "ODBC 32 bit Data Sources ", "MS Access Database ",this.textBox1 .Text, "e:\\temp\\ODBC.INI ");
}
}
}