一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - ASP.NET微信公眾號客服接口

ASP.NET微信公眾號客服接口

2020-04-02 14:50天風隼 ASP.NET教程

這篇文章主要為大家詳細介紹了ASP.NET微信公眾號客服接口的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了ASP.NET微信客服接口的具體代碼,供大家參考,具體內容如下

Kf_account.cs代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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(";內容由程序自動生成,請不要修改此文件內容\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(@"拉取用戶信息的速度取決于你的關注數與網絡速度,
可能需要幾分鐘甚至更長時間。
使用此功能將消耗大量用戶管理接口配額。
要繼續此操作嗎?",
    "提示:", 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(";內容由程序自動生成,請不要修改此文件內容\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("你的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("拉取失敗,返回消息:\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);
    //設定書寫的開始位置為文件的末尾
    fs.Position = fs.Length;
    //將待寫入內容追加到文件末尾
    fs.Write(bytes, 0, bytes.Length);
    fs.Close();
    lab_nums.Text = "已拉取" + i + "個,還剩" + (li_count - i) + "個,請耐心等待";
   }
   lab_nums.Text = "";
   //BindUser();
   btn_GetUser.Enabled = true;
   pictureBox1.Visible = false;
   MessageBox.Show("已全部拉取完畢,請重新打開該窗口");
  }
 
  /// <summary>
  ///  獲取用戶信息詳情,返回json
  /// </summary>
  /// <param name="as_openid"></param>
  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 + "&lang=zh_CN");
   return ls_json;
  }
 
  /// <summary>
  ///  獲取用戶用戶的昵稱
  /// </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的表結構
   foreach (DataRow row in rows)
    tmp.Rows.Add(row.ItemArray); // 將DataRow添加到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 += "<P align=right><FONT size=3>" + ls_isright + "</FONT></P>";
  }
 
  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());
   //客服賬號 設置 xxx@你的公眾號 這樣的格式才是正確的喲。
   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代碼:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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>
 /// <param name="secret">appsecret</param>
 /// <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)//每兩個小時刷新一次
    {
     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方法請求url并接收返回消息
 /// </summary>
 /// <param name="strUrl">Url地址</param>
 /// <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();
   ////在這兒處理返回的文本
   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>
 /// <param name="postData">Post數據</param>
 /// <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);
  // 準備請求...
  try
  {
   // 設置參數
   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();
   //發送請求并獲取相應回應數據
   response = request.GetResponse() as HttpWebResponse;
   //直到request.GetResponse()程序才開始向目標網頁發送Post請求
   instream = response.GetResponseStream();
   sr = new StreamReader(instream, encoding);
   //返回結果網頁(html)代碼
   string content = sr.ReadToEnd();
   string err = string.Empty;
   return content;
  }
  catch (Exception ex)
  {
   string err = ex.Message;
   return string.Empty;
  }
 }
 
 /// <summary>
 /// 格式化字符串
 /// </summary>
 /// <param name="str"></param>
 /// <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>
 /// 用戶名
 /// </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代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
class INIFile
{
 ///// <summary>
 ///// 設置INI文件參數
 ///// </summary>
 ///// <param name="section">INI文件中的段落</param>
 ///// <param name="key">INI文件中的關鍵字</param>
 ///// <param name="val">INI文件中關鍵字的數值</param>
 ///// <param name="filePath">INI文件的完整的路徑和名稱</param>
 ///// <returns></returns>
 //[DllImport("kernel32")]
 //private static extern long WritePrivateProfileString(
 // string section, string key, string val, string filePath);
 
 ///// <summary>
 ///// 獲取INI文件參數
 ///// </summary>
 ///// <param name="section">INI文件中的段落名稱</param>
 ///// <param name="key">INI文件中的關鍵字</param>
 ///// <param name="def">無法讀取時候時候的缺省數值</param>
 ///// <param name="retVal">讀取數值</param>
 ///// <param name="size">數值的大小</param>
 ///// <param name="filePath">INI文件的完整路徑和名稱</param>
 //[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文件中的段落名稱</param>
 ///// <param name="as_key">INI文件中的關鍵字</param>
 ///// <param name="as_FileName">INI文件的完整路徑和名稱</param>
 //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文件中的段落名稱</param>
 ///// <param name="as_key">INI文件中的關鍵字</param>
 ///// <param name="as_FileName">INI文件的完整路徑和名稱</param>
 //public static string GetINIString(string as_section, string as_key)
 //{
 // return GetINIString(as_section, as_key, gs_FileName);
 //}
 
 ///// <summary>
 ///// 設置INI文件參數
 ///// </summary>
 ///// <param name="as_section">INI文件中的段落</param>
 ///// <param name="as_key">INI文件中的關鍵字</param>
 ///// <param name="as_Value">INI文件中關鍵字的數值</param>
 ///// <param name="as_FileName">INI文件的完整路徑和名稱</param>
 //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>
 ///// 設置INI文件參數
 ///// </summary>
 ///// <param name="as_section">INI文件中的段落</param>
 ///// <param name="as_key">INI文件中的關鍵字</param>
 ///// <param name="as_Value">INI文件中關鍵字的數值</param>
 //public static long SetINIString(string as_section, string as_key, string as_Value)
 //{
 // return SetINIString(as_section, as_key, as_Value, gs_FileName);
 //}
 /// <summary>
 /// 寫入INI文件
 /// </summary>
 /// <param name="section">節點名稱[如[TypeName]]</param>
 /// <param name="key">鍵</param>
 /// <param name="val">值</param>
 /// <param name="filepath">文件路徑</param>
 /// <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>
 /// 寫入INI文件(section:節點名稱 key:鍵 val:值)
 /// </summary>
 /// <param name="section">節點名稱</param>
 /// <param name="key">鍵</param>
 /// <param name="val">值</param>
 /// <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">節點名稱</param>
 /// <param name="key">鍵</param>
 /// <param name="def">值</param>
 /// <param name="retval">stringbulider對象</param>
 /// <param name="size">字節大小</param>
 /// <param name="filePath">文件路徑</param>
 /// <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文件名
 
 
 /// <summary>
 /// 讀取INI文件中的內容方法 (Section 節點名稱;key 鍵)
 /// </summary>
 /// <param name="Section">節點名稱</param>
 /// <param name="key">鍵</param>
 /// <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>
 /// 獲取指定小節所有項名和值的一個列表
 /// </summary>
 /// <param name="section">節 段,欲獲取的小節。注意這個字串不區分大小寫</param>
 /// <param name="buffer">緩沖區 返回的是一個二進制的串,字符串之間是用"\0"分隔的</param>
 /// <param name="nSize">緩沖區的大小</param>
 /// <param name="filePath">初始化文件的名字。如沒有指定完整路徑名,windows就在Windows目錄中查找文件</param>
 /// <returns></returns>
 [DllImport("kernel32")]
 public static extern int GetPrivateProfileSection(string section, byte[] buffer, int nSize, string filePath);
 /// <summary>
 /// 獲取指定段section下的所有鍵值對 返回集合的每一個鍵形如"key=value"
 /// </summary>
 /// <param name="section">指定的段落</param>
 /// <param name="filePath">ini文件的絕對路徑</param>
 /// <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'來作為分隔
   {
    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">要寫入的段落名</param>
 /// <param name="key">要刪除的鍵</param>
 /// <param name="fileName">INI文件的完整路徑和文件名</param>
 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>
 /// 返回該配置文件中所有Section名稱的集合
 /// </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;
 }
}

運行結果:

ASP.NET微信公眾號客服接口

ASP.NET微信公眾號客服接口

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: poren18日本老师hd| 福利一区二区在线观看 | 免费在线观看中文字幕 | 92在线视频| 国产精品第2页 | 紧身牛仔裤美女被啪啪久久网 | 黑人巨茎大战欧美白妇 | 美女被狂干 | 日韩欧美亚洲每日更新网 | 欧美久在线观看在线观看 | 成年男女免费大片在线观看 | 99精品久久久久久 | 欧美xbxbxbxb大片 | 亚洲国产精品综合福利专区 | 国产日韩片 | 免费精品99久久国产综合精品 | 亚洲精品第二页 | 舔小说| 日本黄色录像视频 | 国自产精品手机在线视频 | 九九热在线视频观看这里只有精品 | 免费高清资源黄网站在线观看 | 欧美一级视频免费观看 | 99成人国产精品视频 | 欧美免赞性视频 | 日韩在线视频一区二区三区 | 调教女秘书| 亚洲haose在线观看 | 国产91精品久久久久久久 | 国产绳艺在线播放 | 国产一区二区免费不卡在线播放 | 男人j进女屁股视频在线观看 | 91视频一区 | 成人久久18免费网站 | 亚洲欧美视频在线播放 | 亚洲AV福利天堂一区二区三 | 好大水好多好爽好硬好深视频 | 性色AV一区二区三区V视界影院 | 男人把大ji巴放进男人免费视频 | 青柠影视在线播放观看高清 | 91天堂在线视频 |