实例介绍
【实例简介】
宽带账号跑流量工具,下载
【实例截图】
【核心代码】
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace myTools
{
public partial class main : Form
{
private SynchronizationContext _synccontext;
public static List<Tuple<string, string, string>> NetworkList;
public static string newwork = "";
public static bool IsRun = false;
public static int runIndex = 0;
public static int startIndex = 0;
public static bool IsDebug = false;
public static int OnlineDuration = 0;
CancellationTokenSource tsource = new CancellationTokenSource();
List<Task> lt = new List<Task>();
public static int DownloadNum = 1;
public static List<string> ImgUrls = new List<string>(){
"https://sm.myapp.com/original/Office/xmind-8-update8-windows-3.7.8.201807240049.exe",
"https://dl.softmgr.qq.com/original/Audio/iTunes64Setup-12.9.5.7.exe",
"https://sm.myapp.com/original/Development/TArch8.5Try110627.exe",
"https://sm.myapp.com/original/net_app/bluestacks_xiazai-3.1.3.401.exe",
"https://dl.softmgr.qq.com/original/Office/foowwsoft6.13.exe",
"https://dl.softmgr.qq.com/original/Development/jdk-8u191-windows-x64-8.0.1910.12.exe"
};
private string m_networkIndex = "";
public main()
{
InitializeComponent();
_synccontext = SynchronizationContext.Current;
Init();
}
public void Init()
{
this.GetNetworkList();
this.SetDataInit();
CheckForIllegalCrossThreadCalls = false;
this.GetConfig();
if (IsDebug)
this.btn_MacTest.Visible = true;
else
this.btn_MacTest.Visible = false;
}
public void SetDataInit()
{
this.btnReStart.Hide();
this.btnStart.Show();
IsRun = false;
runIndex = 0;
startIndex = 0;
this.lbError.Text = "";
}
public void GetConfig()
{
string _url = System.Configuration.ConfigurationManager.AppSettings["downloadUrl"] ?? "";
if (!string.IsNullOrWhiteSpace(_url))
{
string[] _urls = _url.Split(',');
ImgUrls = _urls.ToList();
}
String _downloadNum = System.Configuration.ConfigurationManager.AppSettings["downloadNum"] ?? "";
if (!string.IsNullOrWhiteSpace(_downloadNum))
{
DownloadNum = Convert.ToInt32(_downloadNum);
}
String _sDebug = System.Configuration.ConfigurationManager.AppSettings["IsDebug"] ?? "";
if (!string.IsNullOrWhiteSpace(_sDebug) && _sDebug == "1")
{
IsDebug = true;
}
String _OnlineDuration = System.Configuration.ConfigurationManager.AppSettings["OnlineDuration"] ?? "";
if (!string.IsNullOrWhiteSpace(_OnlineDuration))
{
OnlineDuration = Convert.ToInt32(_OnlineDuration);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog path = new OpenFileDialog();
path.Title = "请选择Excel文件";
path.Filter = "excel|*.xls;*.xlsx";
path.ShowDialog();
this.txtFileName.Text = path.FileName;
if (!string.IsNullOrWhiteSpace(this.txtFileName.Text))
{
this.ReadExcel();
this.SetDataInit();
}
}
catch (Exception ex)
{
this.lbError.Text = ex.Message;
}
}
private void ReadExcel()
{
DataTable _data = ExcelHelper.ParseAndCheckExcelFile(this.txtFileName.Text);
this.dgvExcel.DataSource = _data;
this.lbTotal.Text = _data.Rows.Count.ToString();
}
private void GetNetworkList()
{
NetworkList = MACHelper.GetAliveNetworks();
List<string> netWorkList = new List<string>();
foreach (var obj in NetworkList)
{
netWorkList.Add(obj.Item3);
}
netWorkList.Insert(0, "请选择网络...");
this.cbNetwork.DataSource = netWorkList;
}
private void cbNetwork_TextChanged(object sender, EventArgs e)
{
if (this.cbNetwork.SelectedValue.ToString() != "请选择网络...")
{
//this.GetMac(this.cbNetwork.SelectedValue.ToString());
string macAddress = "";
newwork = this.cbNetwork.SelectedValue.ToString();
string index = MACHelper.GetAdapterIndex(out macAddress);
foreach (Tuple<string, string, string> obj in NetworkList)
{
if (obj.Item3 == newwork) {
m_networkIndex = obj.Item1;
macAddress = obj.Item2;
break;
}
}
this.txtLocationMac.Text = CorrectMacText(macAddress);
}
}
private string CorrectMacText(string mac)
{
string _mac = mac;
if (_mac == null || _mac.Length != 12)
return "";
int[] _indexs = new int[] { 2, 5, 8, 11, 14 };
for (int i = 0; i < _indexs.Length; i )
{
_mac = _mac.Insert(_indexs[i], "-");
}
return _mac;
}
#region run
private void start()
{
if (string.IsNullOrWhiteSpace(txtFileName.Text))
{
lbError.Text = "请选择Excel文件";
return;
}
string _newwork = this.cbNetwork.SelectedValue.ToString();
if (this.cbNetwork.SelectedValue.ToString() == "请选择网络...")
{
lbError.Text = "请选择网络";
return;
}
IsRun = true;
SetControlDisable();
lbError.Text = "数据正在执行中";
foreach (var item in lt)
{
try
{
item.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void run()
{
try
{
DataTable _data = (DataTable)dgvExcel.DataSource;
for (int i = startIndex; i < _data.Rows.Count; i )
{
string _account = _data.Rows[i][0].ToString();
string _password = _data.Rows[i][1].ToString();
_synccontext.Post(_ => this.txtAccount.Text = _account, null);
_synccontext.Post(_ => this.txtPassword.Text = _password, null);
_synccontext.Post(_ => this.dgvExcel.BindingContext[this.dgvExcel.DataSource].Position = i, null);
//Thread.Sleep(6000);
DoWork(_account, _password);
runIndex ;
_synccontext.Post(_ => this.lbDone.Text = runIndex.ToString(), null);
if (runIndex == _data.Rows.Count)
{
_synccontext.Post(_ => this.lbError.Text = "执行完毕", null);
this.Finish();
}
}
}
catch (Exception ex)
{
_synccontext.Post(_ => this.lbError.Text = ex.Message, null);
}
}
private void DoWork(string account, string password)
{
try
{
List<string> _urls = new List<string>();
int code = -1;
DoNewMac();
Thread.Sleep(3000);
if (MACHelper.NetWorkState(this.cbNetwork.SelectedValue.ToString()))
{
_synccontext.Post(_ => this.lbError.Text = "mac已修改,网络连接中...", null);
SinASDL sinASDL = new SinASDL("宽带连接", account, password);
////拨号上网
code = sinASDL.Connect();
if (code == 0)
{
_synccontext.Post(_ => this.lbError.Text = "ADSL连接成功", null);
//下载点东西吧
_urls = this.DownloadSomething();
//在线时间
if (OnlineDuration != 0)
{
_synccontext.Post(_ => this.lbError.Text = "下载完成,在线等待" OnlineDuration.ToString() "分钟", null);
Thread.Sleep(60000 * OnlineDuration);
}
Thread.Sleep(1000);
sinASDL.Disconnect();
_synccontext.Post(_ => this.lbError.Text = "已断开连接", null);
Thread.Sleep(1000);
}
else
{
_synccontext.Post(_ => this.lbError.Text = "ADSL连接失败" code, null);
Thread.Sleep(3000);
}
LogHelper.SaveLog("info", string.Format("{0};{1};{2}\r\n{3}", account, password, (code == 0 ? "成功" : "失败") code, string.Join(",", _urls)));
}
else
{
LogHelper.SaveLog("info", string.Format("{0};{1};{2};{3}", account, password, "错误", "网卡不可用"));
}
}
catch (Exception ex)
{
LogHelper.SaveLog("info", string.Format("{0};{1};{2};{3}", account, password, "错误", ex.Message));
}
}
private void DoNewMac()
{
string networkName = this.cbNetwork.SelectedValue.ToString();
string newMac = "";
int ii = 1;
while (true)
{
//设置新的mac
ManagementObject mo = MACHelper.NetWork(networkName);
newMac = SetNewMac();
_synccontext.Post(_ => this.lbError.Text = "尝试设置mac:" newMac "----" ii.ToString(), null);
if (MACHelper.NetWorkState(networkName))
{
MACHelper.DisableNetWork(mo);
_synccontext.Post(_ => this.lbError.Text = "网卡已禁用", null);
MACHelper.EnableNetWork(mo);
_synccontext.Post(_ => this.lbError.Text = "网卡正在启用", null);
}
ii ;
int _checkStateCount = 4;
bool isSuccess = false;
while (true)
{
if (_checkStateCount <= 0)
break;
_synccontext.Post(_ => this.lbError.Text = "校验mac地址---" _checkStateCount, null);
if (MACHelper.NetWorkState(networkName))
{
isSuccess = CheckMac(networkName, newMac);
if (isSuccess)
break;
}
Thread.Sleep(500);
_checkStateCount--;
}
if (isSuccess)
{
_synccontext.Post(_ => this.lbError.Text = "设置成功:" newMac, null);
break;
}
else
{
_synccontext.Post(_ => this.lbError.Text = "设置失败,等待重试", null);
continue;
}
}
}
private bool CheckMac(string networkName, string newMac)
{
string nowMac = MACHelper.GetNetworkMac(networkName);
if (string.IsNullOrWhiteSpace(nowMac))
{
return false;
}
if (nowMac == newMac)
{
return true;
}
return false;
}
private List<string> DownloadSomething()
{
List<string> _urls = new List<string>();
for (int i = 0; i < DownloadNum; i )
{
try
{
Random rd = new Random();
int _index = rd.Next(0, ImgUrls.Count - 1);
_urls.Add(ImgUrls[_index]);
WebClient webClient = new WebClient();
_synccontext.Post(_ => this.lbError.Text = "开始下载" (i 1), null);
Byte[] imgData = webClient.DownloadData(ImgUrls[_index]);
Stream ms = new MemoryStream(imgData);
ms.Position = 0;
_synccontext.Post(_ => this.lbError.Text = "结束下载" (i 1), null);
}
catch (Exception ex)
{
_synccontext.Post(_ => this.lbError.Text = ex.Message, null);
}
}
return _urls;
}
private string SetNewMac()
{
String _mac = MACHelper.CreateNewMacAddress();
this.txtRbMac.Text = this.CorrectMacText(_mac);
MACHelper.SetMACAddress(_mac, m_networkIndex);
return _mac;
}
#endregion
private void SetControlDisable()
{
this.btnOpen.Enabled = !IsRun;
if (runIndex > 0)
{
btnStart.Hide();
btnReStart.Show();
}
else
{
btnStart.Show();
btnReStart.Hide();
}
this.btnStart.Enabled = !IsRun;
this.btnReStart.Enabled = !IsRun;
this.btnStop.Enabled = IsRun;
this.cbNetwork.Enabled = !IsRun;
}
private void Finish()
{
this.SetDataInit();
stop();
}
private void stop()
{
//取消线程
tsource.Cancel();
IsRun = false;
SetControlDisable();
}
private void btnStart_Click(object sender, EventArgs e)
{
runIndex = 0;
startIndex = 0;
lt = new List<Task>();
tsource = new CancellationTokenSource();
//创建线程并 加到取消线程对象
Task task = new Task(() =>
{
try
{
if (tsource.IsCancellationRequested == true)
{
tsource.Token.ThrowIfCancellationRequested();
}
run();
}
catch (OperationCanceledException ex)
{
_synccontext.Post(_ => this.lbError.Text = ex.Message, null);
}
}, tsource.Token);
lt.Add(task);
start();
}
private void btnReStart_Click(object sender, EventArgs e)
{
startIndex = runIndex;
lt = new List<Task>();
tsource = new CancellationTokenSource();
//创建线程并 加到取消线程对象
Task task = new Task(() =>
{
try
{
if (tsource.IsCancellationRequested == true)
{
tsource.Token.ThrowIfCancellationRequested();
}
run();
}
catch (OperationCanceledException ex)
{
_synccontext.Post(_ => this.lbError.Text = ex.Message, null);
}
}, tsource.Token);
lt.Add(task);
start();
}
private void btnStop_Click(object sender, EventArgs e)
{
stop();
}
private void btn_MacTest_Click(object sender, EventArgs e)
{
ManagementObject mo = MACHelper.NetWork(this.cbNetwork.SelectedValue.ToString());
SetNewMac();
if (MACHelper.NetWorkState(this.cbNetwork.SelectedValue.ToString()))
{
if (!MACHelper.DisableNetWork(mo))
{
MessageBox.Show("禁用网卡失败!");
}
else
{
MessageBox.Show("禁用网卡成功!");
}
}
else
{
MessageBox.Show("网卡己禁用!");
}
Thread.Sleep(2000);
if (MACHelper.NetWorkState(this.cbNetwork.SelectedValue.ToString()))
{
if (!MACHelper.EnableNetWork(mo))
{
MessageBox.Show("开启网卡失败!");
}
else
{
MessageBox.Show("开启网卡成功!");
}
}
else
{
MessageBox.Show("网卡己开启!");
}
_synccontext.Post(_ => this.lbError.Text = "mac已修改,网络连接中...", null);
}
}
}
标签: 工具
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论