PHP
·
发表于 5年以前
·
阅读量:8301
×i½u£¬¿ª¢Î¢ÐŹ«Öںţ¬¸ºÔ𿪢¿Í*þ¹¦ÄÜ£¬ÕaÀi¼oµ¥¼Ç¼Ï£º
Kf_account.cs´uÂe:
public partial class Kf_account : Form
{
private readonly DataTable adt_user = new DataTable();
private readonly string as_INIFile = Application.StartupPath + "\\user.ini";
public Kf_account()
{
BindUser();
}
private void BindUser()
{
if (!File.Exists(as_INIFile))
{
var str = new StringBuilder();
str.Append(";ÄÚÈÝÓɳÌÐo×Ô¶¯Éu³É£¬Çe²»ÒªÐ޸ĴËÎļþÄÚÈÝ\r\n");
str.Append("[total]\r\n");
str.Append("total=\r\n");
str.Append("[count]\r\n");
str.Append("count=\r\n");
str.Append("[user]\r\n");
//StreamWriter sw = default(StreamWriter);
//sw = File.CreateText(ls_INIFile);
//sw.WriteLine(str.ToString());
//sw.Close();
File.WriteAllText(as_INIFile, str.ToString(), Encoding.Unicode);
File.SetAttributes(as_INIFile, FileAttributes.Hidden);
}
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
Icon = Resource1.ico;
lkl_num.Text = INIFile.ContentValue("total", "total", as_INIFile);
lkl_num_c.Text = INIFile.ContentValue("count", "count", as_INIFile);
pictureBox1.Visible = true;
var sr = new StreamReader(as_INIFile, Encoding.Unicode);
String line;
int li_count = 0;
adt_user.Columns.Clear();
adt_user.Columns.Add("username", Type.GetType("System.String"));
adt_user.Columns.Add("openid", Type.GetType("System.String"));
while ((line = sr.ReadLine()) != null)
{
li_count++;
if (li_count > 6)
{
line = SysVisitor.Current.GetFormatStr(line);
DataRow newRow;
newRow = adt_user.NewRow();
newRow["username"] = line.Substring(0, line.LastIndexOf('='));
newRow["openid"] = line.Substring(line.LastIndexOf('=') + 1);
adt_user.Rows.Add(newRow);
}
}
sr.Close();
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = adt_user;
//dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells;
lbl_count.Text = "¹²" + (li_count - 6) + "ÐÐ";
pictureBox1.Visible = false;
}
private void btn_GetUser_Click(object sender, EventArgs e)
{
if (MessageBox.Show(@"ÀÈ¡Óû§ÐÅÏ¢µÄËÙ¶ÈÈ¡¾oÓÚÄaµÄ¹Ø×¢ÊýÓeÍøÂçËٶȣ¬
¿ÉÄÜÐeÒª¼¸*ÖÖÓÉoÖÁ¸u³¤Ê±¼a¡£
ʹÓô˹¦Äܽ«ÏuºÄ´oÁ¿Óû§¹ÜÀi½Ó¿ÚÅa¶i¡£
Òª¼ÌÐø´Ë²Ù×÷Âð£¿",
"Ìaʾ£º", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
var thr = new Thread(Get_user_list);
thr.Start();
}
private void Get_user_list()
{
File.Delete(as_INIFile);
var str = new StringBuilder();
str.Append(";ÄÚÈÝÓɳÌÐo×Ô¶¯Éu³É£¬Çe²»ÒªÐ޸ĴËÎļþÄÚÈÝ\r\n");
str.Append("[total]\r\n");
str.Append("total=\r\n");
str.Append("[count]\r\n");
str.Append("count=\r\n");
str.Append("[user]\r\n");
File.WriteAllText(as_INIFile, str.ToString(), Encoding.Unicode);
File.SetAttributes(as_INIFile, FileAttributes.Hidden);
string ls_appid = INIFile.ContentValue("weixin", "Appid");
string ls_secret = INIFile.ContentValue("weixin", "AppSecret");
string access_token = "";
string menu = "";
if (ls_appid.Length != 18 || ls_secret.Length != 32)
{
MessageBox.Show("ÄaµÄAppid»oAppSecret²»¶Ô£¬Çe¼i²eºoÔÙ²Ù×÷");
return;
}
access_token = SysVisitor.Current.Get_Access_token(ls_appid, ls_secret);
if (access_token == "")
{
MessageBox.Show("Appid»oAppSecret²»¶Ô£¬Çe¼i²eºoÔÙ²Ù×÷");
return;
}
menu = SysVisitor.Current.GetPageInfo("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token);
if (menu.Substring(2, 7) == "errcode")
{
MessageBox.Show("Àȡʧ°Ü£¬*µ»ØÏuÏ¢£º\r\n" + menu);
}
JObject json = JObject.Parse(menu);
lkl_num.Text = json["total"].ToString();
INIFile.SetINIString("total", "total", lkl_num.Text, as_INIFile);
lkl_num_c.Text = json["count"].ToString();
INIFile.SetINIString("count", "count", lkl_num_c.Text, as_INIFile);
int li_count = int.Parse(json["count"].ToString());
btn_GetUser.Enabled = false;
pictureBox1.Visible = true;
FileStream fs = null;
Encoding encoder = Encoding.Unicode;
for (int i = 0; i < li_count; i++)
{
string openid, username;
openid = Get_UserName(json["data"]["openid"][i].ToString());
username = json["data"]["openid"][i].ToString();
//INIFile.SetINIString("user", openid, username, as_INIFile);
byte[] bytes = encoder.GetBytes(openid + "=" + username + " \r\n");
fs = File.OpenWrite(as_INIFile);
//Ée¶¨ÊeдµÄe_ʼλÖÃΪÎļþµÄÄ(C)β
fs.Position = fs.Length;
//½«´ýдÈeÄÚÈÝ×*¼Óµ½ÎļþÄ(C)β
fs.Write(bytes, 0, bytes.Length);
fs.Close();
lab_nums.Text = "ÒÑÀÈ¡" + i + "¸o£¬»¹Ê£" + (li_count - i) + "¸o£¬ÇeÄÍÐĵȴý";
}
lab_nums.Text = "";
//BindUser();
btn_GetUser.Enabled = true;
pictureBox1.Visible = false;
MessageBox.Show("ÒÑÈ«²¿ÀÈ¡Íe±Ï,ÇeÖØдo¿ª¸Ã´°¿Ú");
}
/// <summary>
/// »ñÈ¡Óû§ÐÅÏ¢ÏeÇe£¬*µ»Øjson
/// </summary>
///<param name="as_openid">
private string Get_User(string as_openid)
{
string ls_json = "";
string access_token = "";
access_token = SysVisitor.Current.Get_Access_token();
ls_json =
SysVisitor.Current.GetPageInfo("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid;=" + as_openid + "〈=zh_CN");
return ls_json;
}
/// <summary>
/// »ñÈ¡Óû§Óû§µÄedzÆ
/// </summary>
private string Get_UserName(string as_openid)
{
string ls_json = "";
ls_json = Get_User(as_openid);
string username = "";
JObject json = JObject.Parse(ls_json);
username = json["nickname"].ToString();
username = SysVisitor.Current.GetFormatStr(username);
return username;
}
private void btn_search_Click(object sender, EventArgs e)
{
string username = txt_search.Text.Trim();
if (string.IsNullOrWhiteSpace(username))
{
return;
}
DataRow[] datarows = adt_user.Select("username like '%" + username + "%'");
var ldt = new DataTable();
ldt.Columns.Clear();
ldt.Columns.Add("username", Type.GetType("System.String"));
ldt.Columns.Add("openid", Type.GetType("System.String"));
ldt = ToDataTable(datarows);
try
{
lbl_count.Text = ldt.Rows.Count.ToString();
}
catch
{
}
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = ldt;
}
public DataTable ToDataTable(DataRow[] rows)
{
if (rows == null || rows.Length == 0) return null;
DataTable tmp = rows[0].Table.Clone(); // ¸´ÖÆDataRowµÄ±i½a¹¹
foreach (DataRow row in rows)
tmp.Rows.Add(row.ItemArray); // ½«DataRowÌi¼Óµ½DataTableÖÐ
return tmp;
}
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
SysVisitor.Current.Wx_openid =
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
SysVisitor.Current.Wx_username =
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
//MessageBox.Show(str);
grb_chat.Enabled = true;
grb_chat.Text = SysVisitor.Current.Wx_username;
}
catch
{
}
webBrowser_msg.DocumentText = "";
string url = string.Format("https://api.weixin.qq.com/cgi-bin/customservice/getrecord?access_token={0}",
SysVisitor.Current.Get_Access_token());
string ls_text = @"{";
ls_text += "\"starttime\" : " + DateTime.Now.AddDays(-3).Ticks + ",";
ls_text += "\"endtime\" : " + DateTime.Now.Ticks + ",";
ls_text += "\"openid\" : \"" + SysVisitor.Current.Wx_openid + "\",";
ls_text += "\"pagesize\" : 1000,";
ls_text += "\"pageindex\" : 1,";
ls_text += "}";
string ls_history = SysVisitor.Current.PostPage(url, ls_text);
webBrowser_msg.DocumentText = ls_history;
}
private void btn_send_Click(object sender, EventArgs e)
{
string ls_msg = richTextBox_msg.Text;
string ls_text = @"{";
ls_text += "\"touser\":\"" + SysVisitor.Current.Wx_openid + "\",";
ls_text += "\"msgtype\":\"text\",";
ls_text += "\"text\":";
ls_text += "{";
ls_text += "\"content\":\"" + ls_msg + "\"";
ls_text += "}";
ls_text += "}";
string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}",
SysVisitor.Current.Get_Access_token());
string ls_isright = SysVisitor.Current.PostPage(url, ls_text);
webBrowser_msg.DocumentText += "
" + ls_isright + "
"; } private void btn_addkf_Click(object sender, EventArgs e) { string url = string.Format("https://api.weixin.qq.com/customservice/kfaccount/add?access_token={0}", SysVisitor.Current.Get_Access_token()); //¿Í*þÕ˺ŠÉeÖà xxx@ÄaµÄ¹«ÖںŠÕaÑuµÄ¸ñʽ²ÅÊÇÕýÈ*µÄÓ´¡£ string ls_text = "{"; ls_text += "\"kf_account\":test2@gz-sisosoft,"; ls_text += "\"nickname\":\"¿Í*þ2\","; ls_text += "\"password\":\"12345\","; ls_text += "}"; string ls_kf = @"{ 'kf_account' : 'test1@gz-sisosoft', 'nickname' : '¿Í*þ1', 'password' : '123456', }"; string ls_isok = SysVisitor.Current.PostPage(url, ls_text); MessageBox.Show(ls_isok); } private void Kf_account_Load(object sender, EventArgs e) { } }
SysVisitor.cs´uÂe:
class SysVisitor
{
private static SysVisitor visit = null;
public static SysVisitor Current
{
get
{
if (visit == null)
visit = new SysVisitor();
return visit;
}
}
/// <summary>
/// »ñÈ¡access_token
/// </summary>
///<param name="appid">appid
///<param name="secret">appsecret
/// <returns></returns>
public string Get_Access_token(string appid, string appsecret)
{
string secondappid = INIFile.ContentValue("weixin", "secondappid");
if (appid.ToLower() == secondappid.ToLower())
{
string ls_time = INIFile.ContentValue("weixin", "gettime");
Decimal ldt;
try
{
ldt = Convert.ToDecimal(ls_time);
if (Convert.ToDecimal(DateTime.Now.ToString("yyyyMMddHHmmss")) - ldt < 7100)//ÿÁ½¸oСʱˢÐÂÒ»´Î
{
return INIFile.ContentValue("weixin", "access_token");
}
}
catch
{ }
}
string ls_appid = appid.Replace(" ", "");
string ls_secret = appsecret.Replace(" ", "");
string access_token = "";
string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid;={0}&secret;={1}", ls_appid, ls_secret);
string json_access_token = GetPageInfo(url);
//DataTable dt = Json.JsonToDataTable(json_access_token);
DataTable dt = JsonHelper.JsonToDataTable(json_access_token);
try
{
access_token = dt.Rows[0]["access_token"].ToString();
}
catch
{
return "";
}
INIFile.SetINIString("weixin", "gettime", DateTime.Now.ToString("yyyyMMddHHmmss"));
INIFile.SetINIString("weixin", "access_token", access_token);
INIFile.SetINIString("weixin", "secondappid", ls_appid);
return access_token;
}
/// <summary>
/// »ñÈ¡access_token
/// </summary>
public string Get_Access_token()
{
string ls_appid = INIFile.ContentValue("weixin", "Appid");
string ls_secret = INIFile.ContentValue("weixin", "AppSecret");
return Get_Access_token(ls_appid, ls_secret);
}
/// <summary>
/// Get*½*¨ÇeÇourl²¢½ÓÊÕ*µ»ØÏuÏ¢
/// </summary>
///<param name="strUrl">UrlµØÖ*
/// <returns></returns>
public string GetPageInfo(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string ret = string.Empty;
Stream s;
string StrDate = "";
string strValue = "";
if (response.StatusCode == HttpStatusCode.OK)
{
s = response.GetResponseStream();
////ÔÚÕa¶u´¦Ài*µ»ØµÄÎı¾
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
//strValue = Reader.ReadToEnd();
}
return strValue;
}
/// <summary>
/// Post*½*¨
/// </summary>
///<param name="posturl">URL
///<param name="postData">PostÊý¾Ý
/// <returns></returns>
public string PostPage(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// ×¼±¸ÇeÇo...
try
{
// ÉeÖòÎÊý
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//*¢ËÍÇeÇo²¢»ñÈ¡ÏaÓ¦»ØÓ¦Êý¾Ý
response = request.GetResponse() as HttpWebResponse;
//Ö±µ½request.GetResponse()³ÌÐo²Å¿ªÊ¼ÏoÄ¿±eÍøÒ³*¢ËÍPostÇeÇo
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//*µ»Ø½a¹uÍøÒ³£¨html£(C)´uÂe
string content = sr.ReadToEnd();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}
/// <summary>
/// ¸ñʽ»¯×Ö*u´®
/// </summary>
///<param name="str">
/// <returns></returns>
public string GetFormatStr(string str)
{
if ("" == str)
return "";
else
{
str = str.Trim();
str = str.Replace("'", "'");
str = str.Replace("¡´", "<");
str = str.Replace("¡µ", ">");
str = str.Replace("£¬", ",");
return str;
}
}
string ls_username = "";
/// <summary>
/// Óû§Ãu
/// </summary>
public string Wx_username
{
get
{
return ls_username;
}
set
{
ls_username = value;
}
}
string ls_openid = "";
/// <summary>
/// Openid
/// </summary>
public string Wx_openid
{
get
{
return ls_openid;
}
set
{
ls_openid = value;
}
}
}
INIFile.cs´uÂe:
class INIFile
{
///// <summary>
///// ÉeÖÃINIÎļþ²ÎÊý
///// </summary>
/////<param name="section">INIÎļþÖеĶÎÂa
/////<param name="key">INIÎļþÖеĹؼu×Ö
/////<param name="val">INIÎļþÖйؼu×ÖµÄÊýÖµ
/////<param name="filePath">INIÎļþµÄÍeÕuµÄÂ*¾¶ºÍÃu³Æ
///// <returns></returns>
//[DllImport("kernel32")]
//private static extern long WritePrivateProfileString(
// string section, string key, string val, string filePath);
///// <summary>
///// »ñÈ¡INIÎļþ²ÎÊý
///// </summary>
/////<param name="section">INIÎļþÖеĶÎÂaÃu³Æ
/////<param name="key">INIÎļþÖеĹؼu×Ö
/////<param name="def">ÎÞ*¨¶ÁȡʱºoʱºoµÄȱʡÊýÖµ
/////<param name="retVal">¶ÁÈ¡ÊýÖµ
/////<param name="size">ÊýÖµµÄ´oС
/////<param name="filePath">INIÎļþµÄÍeÕuÂ*¾¶ºÍÃu³Æ
//[DllImport("kernel32")]
//private static extern int GetPrivateProfileString(
// string section, string key, string def, StringBuilder retVal, int size, string filePath);
//static string gs_FileName = System.AppDomain.CurrentDomain.BaseDirectory + "Config.ini";
///// <summary>
///// »ñÈ¡INIÎļþ²ÎÊý
///// </summary>
/////<param name="as_section">INIÎļþÖеĶÎÂaÃu³Æ
/////<param name="as_key">INIÎļþÖеĹؼu×Ö
/////<param name="as_FileName">INIÎļþµÄÍeÕuÂ*¾¶ºÍÃu³Æ
//public static string GetINIString(string as_section, string as_key, string as_FileName)
//{
// StringBuilder temp = new StringBuilder(255);
// int i = GetPrivateProfileString(as_section, as_key, "", temp, 255, as_FileName);
// return temp.ToString();
//}
///// <summary>
///// »ñÈ¡INIÎļþ²ÎÊý
///// </summary>
/////<param name="as_section">INIÎļþÖеĶÎÂaÃu³Æ
/////<param name="as_key">INIÎļþÖеĹؼu×Ö
/////<param name="as_FileName">INIÎļþµÄÍeÕuÂ*¾¶ºÍÃu³Æ
//public static string GetINIString(string as_section, string as_key)
//{
// return GetINIString(as_section, as_key, gs_FileName);
//}
///// <summary>
///// ÉeÖÃINIÎļþ²ÎÊý
///// </summary>
/////<param name="as_section">INIÎļþÖеĶÎÂa
/////<param name="as_key">INIÎļþÖеĹؼu×Ö
/////<param name="as_Value">INIÎļþÖйؼu×ÖµÄÊýÖµ
/////<param name="as_FileName">INIÎļþµÄÍeÕuÂ*¾¶ºÍÃu³Æ
//public static long SetINIString(string as_section, string as_key, string as_Value, string as_FileName)
//{
// return WritePrivateProfileString(as_section, as_key, as_Value, as_FileName);
//}
///// <summary>
///// ÉeÖÃINIÎļþ²ÎÊý
///// </summary>
/////<param name="as_section">INIÎļþÖеĶÎÂa
/////<param name="as_key">INIÎļþÖеĹؼu×Ö
/////<param name="as_Value">INIÎļþÖйؼu×ÖµÄÊýÖµ
//public static long SetINIString(string as_section, string as_key, string as_Value)
//{
// return SetINIString(as_section, as_key, as_Value, gs_FileName);
//}
/// <summary>
/// дÈeINIÎļþ
/// </summary>
///<param name="section">½ÚµaÃu³Æ[Èç[TypeName]]
///<param name="key">¼u
///<param name="val">Öµ
///<param name="filepath">ÎļþÂ*¾¶
/// <returns></returns>
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);
/// <summary>
/// дÈeINIÎļþ(section:½ÚµaÃu³Æ key:¼u val:Öµ)
/// </summary>
///<param name="section">½ÚµaÃu³Æ
///<param name="key">¼u
///<param name="val">Öµ
/// <returns></returns>
public static long SetINIString(string section, string key, string val, string as_FilePath = "")
{
if (as_FilePath == "")
{
return (WritePrivateProfileString(section, key, val, strFilePath));
}
else
{
return (WritePrivateProfileString(section, key, val, as_FilePath));
}
}
/// <summary>
/// ¶ÁÈ¡INIÎļþ
/// </summary>
///<param name="section">½ÚµaÃu³Æ
///<param name="key">¼u
///<param name="def">Öµ
///<param name="retval">stringbulider¶ÔÏo
///<param name="size">×Ö½Ú´oС
///<param name="filePath">ÎļþÂ*¾¶
/// <returns></returns>
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
public static string strFilePath = Application.StartupPath + "\\Config.ini";//»ñÈ¡INIÎļþĬÈÏÂ*¾¶
public static string strSec = "";
//INIÎļþÃu
/// <summary>
/// ¶ÁÈ¡INIÎļþÖеÄÄÚÈÝ*½*¨ (Section ½ÚµaÃu³Æ;key ¼u)
/// </summary>
///<param name="Section">½ÚµaÃu³Æ
///<param name="key">¼u
/// <returns></returns>
public static string ContentValue(string Section, string key, string as_FilePath = "")
{
StringBuilder temp = new StringBuilder(1024);
if (as_FilePath == "")
{
GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath);
}
else
{
GetPrivateProfileString(Section, key, "", temp, 1024, as_FilePath);
}
return temp.ToString();
}
/// <summary>
/// »ñÈ¡Ö¸¶¨Ð¡½ÚËuÓÐÏiÃuºÍÖµµÄÒ»¸oÁбi
/// </summary>
///<param name="section">½Ú ¶Î£¬Óu»ñÈ¡µÄС½Ú¡£×¢ÒaÕa¸o×Ö´®²»Çø*Ö´oСд
///<param name="buffer">»º³aÇø *µ»ØµÄÊÇÒ»¸o¶þ½øÖƵĴ®£¬×Ö*u´®Ö®¼aÊÇÓÃ"\0"*Ö¸oµÄ
///<param name="nSize">»º³aÇøµÄ´oС
///<param name="filePath">³oʼ»¯ÎļþµÄÃu×Ö¡£ÈçûÓÐÖ¸¶¨ÍeÕuÂ*¾¶Ãu£¬windows¾ÍÔÚWindowsĿ¼ÖвeÕÒÎļþ
/// <returns></returns>
[DllImport("kernel32")]
public static extern int GetPrivateProfileSection(string section, byte[] buffer, int nSize, string filePath);
/// <summary>
/// »ñÈ¡Ö¸¶¨¶ÎsectionϵÄËuÓмuÖµ¶Ô *µ»Ø¼¯ºÏµÄÿһ¸o¼uÐÎÈç"key=value"
/// </summary>
///<param name="section">Ö¸¶¨µÄ¶ÎÂa
///<param name="filePath">iniÎļþµÄ¾ø¶ÔÂ*¾¶
/// <returns></returns>
public static List<string> ReadKeyValues(string section, string as_FilePath = "")
{
byte[] buffer = new byte[32767];
List<string> list = new List<string>();
int length = 0;
if (as_FilePath == "")
{
length = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), strFilePath);
}
else
{
length = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), as_FilePath);
}
string temp;
int postion = 0;
for (int i = 0; i < length; i++)
{
if (buffer[i] == 0x00) //ÒÔ'\0'À´×÷Ϊ*Ö¸o
{
temp = System.Text.ASCIIEncoding.Default.GetString(buffer, postion, i - postion).Trim();
postion = i + 1;
if (temp.Length > 0)
{
list.Add(temp);
}
}
}
return list;
}
/// <summary>
/// ɾ³ýÖ¸¶¨µÄkey
/// </summary>
///<param name="section">ҪдÈeµÄ¶ÎÂaÃu
///<param name="key">Ҫɾ³ýµÄ¼u
///<param name="fileName">INIÎļþµÄÍeÕuÂ*¾¶ºÍÎļþÃu
public static void DelKey(string section, string key, string as_FilePath = "")
{
if (as_FilePath == "")
{
WritePrivateProfileString(section, key, null, strFilePath);
}
else
{
WritePrivateProfileString(section, key, null, as_FilePath);
}
}
/// <summary>
/// *µ»Ø¸ÃÅaÖÃÎļþÖÐËuÓÐSectionÃu³ÆµÄ¼¯ºÏ
/// </summary>
public static ArrayList ReadSections()
{
byte[] buffer = new byte[65535];
int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), strFilePath);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel > 0)
{
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++)
{
if (buffer[iCnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.UTF8.GetString(buffer, iPos, iCnt - iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
}</string></string></string>
ÔËÐнa¹u:
ÕaÀiдͼƬÃeÊo
¸ÐлÔĶÁ£¬Ï£ÍuÄÜ°iÖuµ½´o¼Ò£¬Ð»Ð»´o¼Ò¶Ô±¾Õ¾µÄÖ§³Ö£¡