Exemplo n.º 1
0
        private void BindUser()
        {
            if (!File.Exists(as_INIFile))
            {
                StringBuilder 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);
            }
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.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;
            StreamReader 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('=')).ToString();
                    newRow["openid"]   = line.Substring(line.LastIndexOf('=') + 1).ToString();
                    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;
        }
Exemplo n.º 2
0
        private void Get_user_list()
        {
            File.Delete(as_INIFile);
            StringBuilder 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("已全部拉取完毕,请重新打开该窗口");
        }