- UID
- 6847
- 积分
- 1065
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-6-23
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
using System;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System.ComponentModel;
public class IniHelper
{
[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);
/// <summary>
/// 读取ini文件
/// </summary>
/// <param name="Section">名称</param>
/// <param name="Key">关键字</param>
/// <param name="defaultText">默认值</param>
/// <param name="iniFilePath">ini文件地址</param>
/// <returns></returns>
[LispFunction("zgx:ini:getvalue")]
public static string GetValue(ResultBuffer rb)
{
TypedValue[] TB = rb.AsArray();
string Section, Key, defaultText, iniFilePath;
iniFilePath = TB[0].Value as string;
Section = TB[1].Value as string;
Key = TB[2].Value as string;
defaultText = null;
if (File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section, Key, defaultText, temp, 1024, iniFilePath);
if (temp.ToString() == "")
{ return null; }
else
{ return temp.ToString(); }
}
else
{
return defaultText;
}
}
/// <summary>
/// 写入ini文件
/// </summary>
/// <param name="Section">名称</param>
/// <param name="Key">关键字</param>
/// <param name="defaultText">默认值</param>
/// <param name="iniFilePath">ini文件地址</param>
/// <returns></returns>
[LispFunction("zgx:ini:setvalue")]
public static bool SetValue(ResultBuffer rb)
//(string Section, string Key, string Value, string iniFilePath)
{
TypedValue[] TB = rb.AsArray();
//string Section, Key, defaultText, iniFilePath;
string iniFilePath = TB[0].Value as string;
string Section = TB[1].Value as string;
string Key = TB[2].Value as string;
string Value = TB[3].Value as string;
var pat = Path.GetDirectoryName(iniFilePath);
if (Directory.Exists(pat) == false)
{
Directory.CreateDirectory(pat);
}
if (File.Exists(iniFilePath) == false)
{
File.Create(iniFilePath).Close();
}
long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
if (OpStation == 0)
{
return false;
}
else
{
return true;
}
}
}
|
|