菜鸟请教关于调取ini配置文件问题。
现在写了一个Excel.ini的配置文件。
[Section1]
A = 姓名
B = 性别
C = 年龄
D = 测试
我现在想SELECT出来用配置文件控制该怎么写啊,看了好久没想出来办法~求大神教学。
string strCom = "SELECT A,C,B FROM [Sheet1$]";
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
namespace ConsoleApplicationCsdn
{
class Program
{
static void Main(string[] args)
{
string A = OperateIniFile.ReadIniData("Section1", "A", "", @"C:\Users\myx\Desktop\Excel.ini");
//姓名
}
}
public class OperateIniFile
{
#region API函数声明
[DllImport("kernel32")]//返回0表示失败,非0为成功
private static extern long WritePrivateProfileString(string section,string key,
string val,string filePath);
[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section,string key,
string def,StringBuilder retVal,int size,string filePath);
#endregion
#region 读Ini文件
public static string ReadIniData(string Section,string Key,string NoText,string iniFilePath)
{
if(File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);
return temp.ToString();
}
else
{
return String.Empty;
}
}
#endregion
}
}
//写一个ini类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace F6
{
class INI
{
//声明INI文件的写操作函数 WritePrivateProfileString()
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
// 声明INI文件的读操作函数 GetPrivateProfileString()
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
private string sPath = null;
public INI(string path)
{
this.sPath = path;
}
public void Writue(string section, string key, string value)
{
// section=配置节,key=键名,value=键值,path=路径
WritePrivateProfileString(section, key, value, sPath);
}
public string ReadValue(string section, string key)
{
// 每次从ini中读取多少字节
System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
// section=配置节,key=键名,temp=上面,path=路径
GetPrivateProfileString(section, key, "", temp, 255, sPath);
return temp.ToString();
}
}
}
//从excel.ini读取类
public static string Current = Directory.GetCurrentDirectory();
public static INI ini = new INI(Current + "/Excel.ini");
public static string name = ini.ReadValue("Section1", "A");
public static string sex = ini.ReadValue("Section1", "B");
public static string age = ini.ReadValue("Section1", "C");
public static string test = ini.ReadValue("Section1", "D");