C# 怎样创建INI文件到安装目录下 ,安装地址是不确定的
2个回答
展开全部
创建INI文件,创建完成之后在启动文件的目录下
public class IniOP
{
#region 声明读写INI文件的API函数
[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);
#endregion
#region 变量声明
//初始化路径名,主键名,字符串长度,这里需要加一个说明,在对应函数才需要做类似操作,初始值应该为空
private string m_strFilePath = "";
private string m_strKey = "";
private string m_strSubKey = "";
private int m_intSize = 255;
private string _strFilePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Default.ini";
private string _strKey = "ErrorKeyIsNull";
private string _strSubKey = "ErrorSubKeyIsNull";
#endregion
#region 属性设置
//设置路径属性
public string FilePath { get { return m_strFilePath; } set { m_strFilePath = value; } }
//设置主键名属性
public string Key { get { return m_strKey; } set { m_strKey = value; } }
//设置字符串长度属性
public int Size { get { return m_intSize; } set { m_intSize = value; } }
//设置子键名属性
public string SubKey { get { return m_strSubKey; } set { m_strSubKey = value; } }
//版本信息
public static string Version
{
get
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
#endregion
#region 构造函数
//构造函数,无参数
public IniOP()
{
}
//构造函数,路径名
public IniOP(string strPath)
{
m_strFilePath = strPath;
}
//构造函数,路径名,主键名
public IniOP(string strPath, string strKey)
{
m_strFilePath = strPath;
m_strKey = strKey;
}
//构造函数,路径名,主键名,字符串长度
public IniOP(string strPath, string strKey, int intSize)
{
m_strFilePath = strPath;
m_strKey = strKey;
m_intSize = intSize;
}
//构造函数,路径名,主键名,字符串长度,子键名
public IniOP(string strPath, string strKey, int intSize, string strSubKey)
{
m_strFilePath = strPath;
m_strKey = strKey;
m_intSize = intSize;
m_strSubKey = strSubKey;
}
#endregion
#region 写入ini文件时的默认值判断
private void IniCheckDefaultValue()
{
if (m_strFilePath == "" || System.IO.File.Exists(m_strFilePath) == false)
{
_strFilePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Default.ini";
}
else
{
_strFilePath = m_strFilePath;
}
if (m_strKey == "")
{
_strKey = "ErrorKeyIsNull_" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
}
else
{
_strKey = m_strKey;
}
if (m_strSubKey == "")
{
_strSubKey = "ErrorSubKeyIsNull_" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
}
else
{
_strSubKey = m_strSubKey;
}
}
#endregion
#region 写入文件函数(非静态)
//写INI文件,输入键值
public void WriteValue(string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(_strKey, _strSubKey, strValue, _strFilePath);
}
//写INI文件,输入子键名,键值
public void WriteValue(string strSubKey, string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(_strKey, strSubKey, strValue, _strFilePath);
}
//写INI文件,输入主键名,子键名,键值
public void WriteValue(string strKey, string strSubKey, string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(strKey, strSubKey, strValue, _strFilePath);
}
//写INI文件,输入主键名,子键名,文件路径,键值
public void WriteValue(string strKey, string strSubKey, string strPath, string strValue)
{
WritePrivateProfileString(strKey, strSubKey, strValue, strPath);
}
#endregion
#region 写入文件函数(静态)
//写INI文件,输入主键名,子键名,文件路径,键值
public static void staticWriteValue(string strKey, string strSubKey, string strPath, string strValue)
{
WritePrivateProfileString(strKey, strSubKey, strValue, strPath);
}
#endregion
#region 读取文件函数(非静态)
//读取INI文件,所有参数提前设置好
public string ReadValue()
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(m_strKey, m_strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入子键名
public string ReadValue(string strSubKey)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(m_strKey, strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名
public string ReadValue(string strKey, string strSubKey)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,文件路径
public string ReadValue(string strKey, string strSubKey, string strPath)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, m_intSize, strPath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,取字符串长度
public string ReadValue(string strKey, string strSubKey, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,文件路径,取字符串长度
public string ReadValue(string strKey, string strSubKey, string strPath, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, strPath);
return strTemp.ToString();
}
#endregion
#region 读取文件函数(静态)
//读取INI文件,输入主键名,子键名,文件路径,取字符串长度
public static string staticReadValue(string strKey, string strSubKey, string strPath, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, strPath);
return strTemp.ToString();
}
#endregion
}
public class IniOP
{
#region 声明读写INI文件的API函数
[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);
#endregion
#region 变量声明
//初始化路径名,主键名,字符串长度,这里需要加一个说明,在对应函数才需要做类似操作,初始值应该为空
private string m_strFilePath = "";
private string m_strKey = "";
private string m_strSubKey = "";
private int m_intSize = 255;
private string _strFilePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Default.ini";
private string _strKey = "ErrorKeyIsNull";
private string _strSubKey = "ErrorSubKeyIsNull";
#endregion
#region 属性设置
//设置路径属性
public string FilePath { get { return m_strFilePath; } set { m_strFilePath = value; } }
//设置主键名属性
public string Key { get { return m_strKey; } set { m_strKey = value; } }
//设置字符串长度属性
public int Size { get { return m_intSize; } set { m_intSize = value; } }
//设置子键名属性
public string SubKey { get { return m_strSubKey; } set { m_strSubKey = value; } }
//版本信息
public static string Version
{
get
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
#endregion
#region 构造函数
//构造函数,无参数
public IniOP()
{
}
//构造函数,路径名
public IniOP(string strPath)
{
m_strFilePath = strPath;
}
//构造函数,路径名,主键名
public IniOP(string strPath, string strKey)
{
m_strFilePath = strPath;
m_strKey = strKey;
}
//构造函数,路径名,主键名,字符串长度
public IniOP(string strPath, string strKey, int intSize)
{
m_strFilePath = strPath;
m_strKey = strKey;
m_intSize = intSize;
}
//构造函数,路径名,主键名,字符串长度,子键名
public IniOP(string strPath, string strKey, int intSize, string strSubKey)
{
m_strFilePath = strPath;
m_strKey = strKey;
m_intSize = intSize;
m_strSubKey = strSubKey;
}
#endregion
#region 写入ini文件时的默认值判断
private void IniCheckDefaultValue()
{
if (m_strFilePath == "" || System.IO.File.Exists(m_strFilePath) == false)
{
_strFilePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Default.ini";
}
else
{
_strFilePath = m_strFilePath;
}
if (m_strKey == "")
{
_strKey = "ErrorKeyIsNull_" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
}
else
{
_strKey = m_strKey;
}
if (m_strSubKey == "")
{
_strSubKey = "ErrorSubKeyIsNull_" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
}
else
{
_strSubKey = m_strSubKey;
}
}
#endregion
#region 写入文件函数(非静态)
//写INI文件,输入键值
public void WriteValue(string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(_strKey, _strSubKey, strValue, _strFilePath);
}
//写INI文件,输入子键名,键值
public void WriteValue(string strSubKey, string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(_strKey, strSubKey, strValue, _strFilePath);
}
//写INI文件,输入主键名,子键名,键值
public void WriteValue(string strKey, string strSubKey, string strValue)
{
IniCheckDefaultValue();
WritePrivateProfileString(strKey, strSubKey, strValue, _strFilePath);
}
//写INI文件,输入主键名,子键名,文件路径,键值
public void WriteValue(string strKey, string strSubKey, string strPath, string strValue)
{
WritePrivateProfileString(strKey, strSubKey, strValue, strPath);
}
#endregion
#region 写入文件函数(静态)
//写INI文件,输入主键名,子键名,文件路径,键值
public static void staticWriteValue(string strKey, string strSubKey, string strPath, string strValue)
{
WritePrivateProfileString(strKey, strSubKey, strValue, strPath);
}
#endregion
#region 读取文件函数(非静态)
//读取INI文件,所有参数提前设置好
public string ReadValue()
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(m_strKey, m_strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入子键名
public string ReadValue(string strSubKey)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(m_strKey, strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名
public string ReadValue(string strKey, string strSubKey)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, m_intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,文件路径
public string ReadValue(string strKey, string strSubKey, string strPath)
{
StringBuilder strTemp = new StringBuilder(m_intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, m_intSize, strPath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,取字符串长度
public string ReadValue(string strKey, string strSubKey, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, m_strFilePath);
return strTemp.ToString();
}
//读取INI文件,输入主键名,子键名,文件路径,取字符串长度
public string ReadValue(string strKey, string strSubKey, string strPath, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, strPath);
return strTemp.ToString();
}
#endregion
#region 读取文件函数(静态)
//读取INI文件,输入主键名,子键名,文件路径,取字符串长度
public static string staticReadValue(string strKey, string strSubKey, string strPath, int intSize)
{
StringBuilder strTemp = new StringBuilder(intSize);
int i = GetPrivateProfileString(strKey, strSubKey, "", strTemp, intSize, strPath);
return strTemp.ToString();
}
#endregion
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询