日期:2014-05-18 浏览次数:20999 次
#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文件操作
//读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;
}
}
//写Ini文件
public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
{
if (!File.Exists(iniFilePath))
{
//如果不存在该文件,创建它
Utils.WriteFile(iniFilePath, "");
}
long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
if (OpStation == 0)
{
return false;
}
else
{
return true;
}
}
#endregion
#region 本地文件操作
//读文件
public static string ReadFile(string path)
{
if (File.Exists(path))
{
//如果文件存在
string test = File.ReadAllText(path, Encoding.Default);
return test;
}
else
{
return String.Empty;
}
}
//写文件
public static void WriteFile(string path, string content)
{
//增加,如果没有该文件,创建它再增加
File.AppendAllText(path, content);
}
//创建目录
public static void WriteDir(string path)
{
// Check to see if a directory exists
bool dirExists = Directory.Exists(path);
//目录不存在
if (!dirExists)
Directory.CreateDirectory(path);
}
//删除文件
public static void DelFile(string path)
{
File.Delete(path);
}
#endregion