最近,开发微信公众号,负责开发客服功能,这里简单记录下:
Kf_account.cs代码:
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(";内容由程序自动生成,请不要修改此文件内容
");
str.Append("[total]
");
str.Append("total=
");
str.Append("[count]
");
str.Append("count=
");
str.Append("[user]
");
//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(@"拉取用户信息的速度取决于你的关注数与网络速度,
可能需要几分钟甚至更长时间。
使用此功能将消耗大量用户管理接口配额。
要继续此操作吗?",
"提示:", 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(";内容由程序自动生成,请不要修改此文件内容
");
str.Append("[total]
");
str.Append("total=
");
str.Append("[count]
");
str.Append("count=
");
str.Append("[user]
");
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("你的Appid或AppSecret不对,请检查后再操作");
return;
}
access_token = SysVisitor.Current.Get_Access_token(ls_appid, ls_secret);
if (access_token == "")
{
MessageBox.Show("Appid或AppSecret不对,请检查后再操作");
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("拉取失败,返回消息:
" + 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 <p></p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/learn/5240" title="TP5.1微信公众号开发源码与课件"><img
src="https://img.php.cn/upload/webcode/000/013/745/5b2a0c677551d677.jpg" alt="TP5.1微信公众号开发源码与课件" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/learn/5240" title="TP5.1微信公众号开发源码与课件" class="overflowclass">TP5.1微信公众号开发源码与课件</a>
<p class="overflowclass">微信公众号开发源码与课件</p>
</div>
<a rel="nofollow" href="/xiazai/learn/5240" title="TP5.1微信公众号开发源码与课件" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p><strong>SysVisitor.cs代码:</strong></p><p class="phpcode"></p><pre class="brush:php;toolbar:false">class SysVisitor
{
private static SysVisitor visit = null;
public static SysVisitor Current
{
get
{
if (visit == null)
visit = new SysVisitor();
return visit;
}
}
////// 获取access_token
//////appid
///appsecret
///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 ");
str = str.Replace(",", ",");
return str;
}
}
string ls_username = "";
////// 用户名
///public string Wx_username
{
get
{
return ls_username;
}
set
{
ls_username = value;
}
}
string ls_openid = "";
////// Openid
///public string Wx_openid
{
get
{
return ls_openid;
}
set
{
ls_openid = value;
}
}
}
INIFile.cs代码:
class INIFile
{
////////// 设置INI文件参数
//////////INI文件中的段落
/////INI文件中的关键字
/////INI文件中关键字的数值
/////INI文件的完整的路径和名称
///////[DllImport("kernel32")]
//private static extern long WritePrivateProfileString(
// string section, string key, string val, string filePath);
////////// 获取INI文件参数
//////////INI文件中的段落名称
/////INI文件中的关键字
/////无法读取时候时候的缺省数值
/////读取数值
/////数值的大小
/////INI文件的完整路径和名称
//[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";
////////// 获取INI文件参数
//////////INI文件中的段落名称
/////INI文件中的关键字
/////INI文件的完整路径和名称
//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();
//}
////////// 获取INI文件参数
//////////INI文件中的段落名称
/////INI文件中的关键字
/////INI文件的完整路径和名称
//public static string GetINIString(string as_section, string as_key)
//{
// return GetINIString(as_section, as_key, gs_FileName);
//}
////////// 设置INI文件参数
//////////INI文件中的段落
/////INI文件中的关键字
/////INI文件中关键字的数值
/////INI文件的完整路径和名称
//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);
//}
////////// 设置INI文件参数
//////////INI文件中的段落
/////INI文件中的关键字
/////INI文件中关键字的数值
//public static long SetINIString(string as_section, string as_key, string as_Value)
//{
// return SetINIString(as_section, as_key, as_Value, gs_FileName);
//}
////// 写入INI文件
//////节点名称[如[TypeName]]
///键
///值
///文件路径
///[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);
////// 写入INI文件(section:节点名称 key:键 val:值)
//////节点名称
///键
///值
///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));
}
}
////// 读取INI文件
//////节点名称
///键
///值
///stringbulider对象
///字节大小
///文件路径
///[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文件名
////// 读取INI文件中的内容方法 (Section 节点名称;key 键)
//////节点名称
///键
///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();
}
////// 获取指定小节所有项名和值的一个列表
//////节 段,欲获取的小节。注意这个字串不区分大小写
///缓冲区 返回的是一个二进制的串,字符串之间是用" "分隔的
///缓冲区的大小
///初始化文件的名字。如没有指定完整路径名,windows就在Windows目录中查找文件
///[DllImport("kernel32")]
public static extern int GetPrivateProfileSection(string section, byte[] buffer, int nSize, string filePath);
////// 获取指定段section下的所有键值对 返回集合的每一个键形如"key=value"
//////指定的段落
///ini文件的绝对路径
///public static ListReadKeyValues(string section, string as_FilePath = "")
{
byte[] buffer = new byte[32767];
Listlist = new List();
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 0)
{
list.Add(temp);
}
}
}
return list;
}
////// 删除指定的key
//////要写入的段落名
///要删除的键
///INI文件的完整路径和文件名
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);
}
}
////// 返回该配置文件中所有Section名称的集合
///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 <p></p><p>运行结果:</p><p><img src="http://files.php.net/file_images/article/201610/20161021111209556.png?2016921111241244?x-oss-process=image/resize,p_40" alt=""></p><p>这里写图片描述<br></p><p><img src="http://files.php.net/file_images/article/201610/20161021111323023.jpg?2016921111339?x-oss-process=image/resize,p_40" alt=""></p><p>感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!</p>
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










