C# Exe + Web 自動化 (BitComet 綠燈 自動化配置、設置)

BitComet GreenLight,內網黃燈轉綠燈 (HighID), 增加p2p連接率提速下載-CSDN博客
前兩天寫個這個,每次開機關機后要重來一遍很麻煩的索性寫個自動化。


先還是按照上面的教程自己制作一遍,留下Luck?以及?路由器相關的?端口記錄信息。

(因為自動化我沒寫創建的邏輯,只是寫了更改端口號的邏輯,所以基礎信息條目需要存在。)

基于更改信息,自動化主要邏輯如下

1、取得Luck?設定好的端口

2、復制到路由器相關端口映射頁面保存

3、啟動BT,設置新的端口映射數據


完整代碼如下:

 public class NetworkManagerExt{private string HostPort { get; set; }private string RemotePort { get; set; }private const string BitCometPath = @"D:\Program Files\BitComet\BitComet.exe";private const string LuckyPath = @"D:\Lucky\lucky.exe";private const string RouterUrl = "http://192.168.0.1/";private const string RouterPassword = "你自己的密碼";private const string LuckyUrl = "http://127.0.0.1:16601/#/login";private const string LuckyPassword = "666"; //luck 默認密碼private const int DefaultTimeoutSeconds = 30;public void ConfigureNetwork(){StartLuckyAndConfigureRouter();}public void StartBitComet(){if (!string.IsNullOrEmpty(RemotePort)){ConfigureBitComet(RemotePort);}else{Console.WriteLine("Error: RemotePort not set. Cannot configure BitComet.");}}private Process GetOrStartProcess(string exePath){string processName = System.IO.Path.GetFileNameWithoutExtension(exePath);Process[] processes = Process.GetProcessesByName(processName);return processes.Length > 0 ? processes[0] : Process.Start(exePath);}private void ConfigureBitComet(string port){Process calc = Process.Start(BitCometPath);AutomationElement mainExe = null;// 等待BitComet窗口出現DateTime startTime = DateTime.Now;TimeSpan timeout = TimeSpan.FromSeconds(DefaultTimeoutSeconds);while (mainExe == null){if (DateTime.Now - startTime > timeout){throw new TimeoutException("Timeout waiting for BitComet window to appear");}AutomationElementCollection elementCollection = AutomationElement.RootElement.FindAll(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));foreach (AutomationElement item in elementCollection){if (item.Current.Name.Contains("BitComet", StringComparison.OrdinalIgnoreCase)){mainExe = item;break;}}if (mainExe == null){System.Threading.Thread.Sleep(500); // 短暫等待后重試}}WindowPattern windowPattern = (WindowPattern)mainExe.GetCurrentPattern(WindowPattern.Pattern);windowPattern.SetWindowVisualState(WindowVisualState.Normal);System.Windows.Forms.SendKeys.SendWait("^p");bool setOperated = false;startTime = DateTime.Now;while (!setOperated){if (DateTime.Now - startTime > timeout){throw new TimeoutException("Timeout waiting for BitComet settings dialog");}var tempWindow = mainExe.FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));if (tempWindow != null){var tempPane = tempWindow.FindFirst(TreeScope.Subtree,new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));var onButtonPane = tempWindow.FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));if (tempPane != null){ValuePattern valuePattern = (ValuePattern)tempPane.GetCurrentPattern(ValuePattern.Pattern);valuePattern.SetValue(port);setOperated = true;InvokePattern invokePattern = (InvokePattern)onButtonPane.GetCurrentPattern(InvokePattern.Pattern);invokePattern.Invoke();}}if (!setOperated){System.Threading.Thread.Sleep(500); // 短暫等待后重試}}}private void StartLuckyAndConfigureRouter(){Process calc = GetOrStartProcess(LuckyPath);AutomationElement mainExe = null;// 等待Lucky窗口出現DateTime startTime = DateTime.Now;TimeSpan timeout = TimeSpan.FromSeconds(DefaultTimeoutSeconds);while (mainExe == null){if (DateTime.Now - startTime > timeout){throw new TimeoutException("Timeout waiting for Lucky window to appear");}var element = AutomationElement.RootElement.FindFirst(TreeScope.Subtree,new PropertyCondition(AutomationElement.NameProperty, "萬吉"));if (element != null){mainExe = element;}else{System.Threading.Thread.Sleep(500); // 短暫等待后重試}}using (IWebDriver driver = new EdgeDriver()){// 設置隱式等待,適用于所有元素查找driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);// 創建顯式等待對象WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(DefaultTimeoutSeconds));// 登錄Luckydriver.Navigate().GoToUrl(LuckyUrl);// 等待輸入框出現并輸入密碼var inputElements = wait.Until(d => d.FindElements(By.CssSelector("input[placeholder='默認666']")));foreach (var item in inputElements){item.Clear();item.SendKeys(LuckyPassword);}// 等待登錄按鈕出現并點擊IWebElement loginButton = wait.Until(d => {var button = d.FindElement(By.CssSelector("button.el-button.el-button--primary.is-round"));return button.Text == "登錄" ? button : null;});loginButton.Click();// 獲取端口信息driver.Navigate().GoToUrl("http://127.0.0.1:16601/#/stun");// 等待第一個IP端口信息出現IWebElement firstIpSpan = wait.Until(d => d.FindElement(By.XPath("/html/body/div[1]/div/section/section/section/main/div/div/div[1]/div/div[1]/div[1]/div[1]/div/div/div/div/table/tbody/tr[2]/td[2]/span[1]/span")));// 等待第二個IP端口信息出現IWebElement secondIpSpan = wait.Until(d => d.FindElement(By.XPath("/html/body/div[1]/div/section/section/section/main/div/div/div[1]/div/div[1]/div[1]/div[1]/div/div/div/div/table/tbody/tr[2]/td[2]/span[3]/span")));string firstIpPort = firstIpSpan.Text;string secondIpPort = secondIpSpan.Text;if (!string.IsNullOrEmpty(firstIpPort) && !string.IsNullOrEmpty(secondIpPort)){HostPort = firstIpPort.Split(':')[1];RemotePort = secondIpPort.Split(':')[1];// 配置路由器ConfigureRouter(driver, wait);}}}private void ConfigureRouter(IWebDriver driver, WebDriverWait wait){driver.Navigate().GoToUrl(RouterUrl);// 等待密碼輸入框出現IWebElement routerPwd = wait.Until(d => d.FindElement(By.XPath("/html/body/div[6]/div[2]/ul/li[2]/ul/li[1]/input")));routerPwd.Clear();routerPwd.SendKeys(RouterPassword);// 等待登錄按鈕出現并點擊IWebElement loginButton = wait.Until(d => d.FindElement(By.XPath("/html/body/div[6]/div[2]/ul/li[3]/input")));loginButton.Click();// 等待功能1按鈕出現并點擊IWebElement func1 = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[2]/ul/li[3]/div/i[2]")));func1.Click();// 等待功能2按鈕出現并點擊IWebElement func2 = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div/div[2]/div[1]/div[8]/div/div/input[3]")));func2.Click();// 等待功能3按鈕出現并點擊IWebElement func3 = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[2]/td[7]/i")));func3.Click();// 設置外部端口IWebElement outport = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[2]/td[3]/input")));outport.Clear();outport.SendKeys(HostPort);// 設置內部端口IWebElement selfport = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[2]/td[4]/input")));selfport.Clear();selfport.SendKeys(RemotePort);// 保存路由器設置IWebElement saveButton = wait.Until(d => d.FindElement(By.XPath("/html/body/div[3]/div[2]/div[1]/div[3]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[2]/td[7]/input[1]")));saveButton.Click();}}

路由器 Web?部分(ConfigureRouter()方法),需要自行Web?頁面?元素?XPath?取得慢慢調整,我的路由器是?TPLink ,TL-WDR8500,配置界面大致如下:

調用代碼
?

   static void Main(string[] args){var networkManager = new NetworkManagerExt();networkManager.ConfigureNetwork();networkManager.StartBitComet();}

這樣?每次開機執行一下這個exe,保障BT?是綠燈,前面配置設置那些步驟全自動化。

需要的包和引用如下:(基于?.net 9.0?編譯通過,測試通過)

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable><ApplicationManifest>app.manifest</ApplicationManifest></PropertyGroup><ItemGroup><FrameworkReference Include="Microsoft.WindowsDesktop.App" /><PackageReference Include="Selenium.WebDriver" Version="4.29.0" /></ItemGroup>
</Project>



?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/72425.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/72425.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/72425.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于 Docker 搭建 FRP 內網穿透開源項目

有些配置項不知道該不該用,不知道該在哪用,不知道怎么用,所以我自己寫個文章簡單記錄一下做個筆記 本文介紹的是基于 Docker 運行 frps 和 frpc,并通過 TCP 協議簡單穿透 SSH 和 HTTP,在觀看本文之前請確保你的機器已經安裝 Docker 服務端搭建 frps# 連接擁有公網 IP 的…

python---序列 (str,list,tuple)

一、 序列類型入門 python的數據類型&#xff1a;int float bool str 運算符 - * / % > < and or not 流程控制ifelsewhilefor掌握python的2大容器類型數值類型&#xff08;3個&#xff09;&#xff1a;int float bool序列類型容器(3個)&#xff1a;str &#xff1a; …

CSS元素層疊順序規則

CSS元素層疊順序規則 看圖說話總結: background/borderz-index(<0)blockfloatinline/inline-blockz-index(0,auto)z-index (>0)

刪除有序數組中的重復項(26)

26. 刪除有序數組中的重復項 - 力扣&#xff08;LeetCode&#xff09; 解法&#xff1a; class Solution { public:int removeDuplicates(vector<int>& nums) {auto first nums.begin();auto last nums.end();auto result first;if (first last) {return std::…

Vue 概念、歷史、發展和Vue簡介

一、Vue概念 官方定義&#xff1a; 漸進式JavaScript 框架&#xff0c;易學易用&#xff0c;性能出色&#xff0c;適用場景豐富的 Web 前端框架。 Vue.js 是一個流行的前端JavaScript框架&#xff0c;由尤雨溪&#xff08;Evan You&#xff09;開發并維護。 它最初于2014年發…

ArcGIS Pro將有文字標注底圖切換為無標注底圖(在線地圖圖源)

今天介紹一下在ArcGIS Pro將有標注的地形底圖換成無標注的底圖。 大家在這項目底圖時候會經常調用ArcGIS Pro自帶的地形圖&#xff0c;但是這個地形圖自帶是有注記的&#xff0c;如下圖。 如何更改&#xff0c;才可以調用無文字注記的呢&#xff1f; 對于一個已經切好圖的有注記…

Xxl-Job學習筆記

目錄 概述 核心架構 核心特點 應用場景 什么是任務調度 快速入門 獲取源碼 初始化調度數據庫 基本配置 數據源datasource 郵箱email&#xff08;可選&#xff09; 會話令牌access token 啟動調度中心 啟動執行器 依賴 yaml基本配置 XxlJobConfig類配置 定義執…

讓雙向鏈表不在云里霧里

又來博客留下我的足跡了&#xff0c;哈哈哈&#xff0c;這次是對于雙向鏈表的理解 目錄 創建雙向鏈表&#xff1a; 申請結點&#xff1a; 雙向鏈表初始化&#xff1a; 雙向鏈表插入結點&#xff1a; 雙向鏈表刪除結點&#xff1a; 雙向鏈表的打印&#xff1a; 雙向鏈表…

java虛擬機(JVM)以及各種參數詳解

Java 虛擬機&#xff08;JVM&#xff09;提供了許多參數來調整其行為和性能&#xff0c;以便更好地適應不同的應用場景。理解和使用這些參數對于優化 Java 應用程序的性能非常重要。以下是一些常用的 JVM 參數及其詳細說明&#xff1a; 1. 內存管理參數 -Xms<size>&…

如何搭配 AI 量化策略選股

AI 量化選股策略結合了 技術指標、基本面數據、市場情緒&#xff0c;利用 機器學習、深度學習、因子分析 等方法&#xff0c;提高選股精準度和交易決策效率。下面介紹 如何搭配 AI 量化策略選股。 1. AI 量化選股的核心方法 AI 量化選股主要依靠 數據驅動&#xff0c;包括&…

Python 爬蟲:一文掌握 SVG 映射反爬蟲

更多內容請見: 爬蟲和逆向教程-專欄介紹和目錄 文章目錄 1. SVG 概述1.1 SVG的優點1.1 映射反爬蟲的原理2. SVG 映射反爬蟲的示例3. 應對 SVG 映射反爬蟲的方法3.1 解析 SVG 圖像3.2 處理自定義字體3.3 使用 OCR 技術3.4 動態生成 SVG 的處理4. 實戰案例4.1 使用 SVG 映射顯示…

前端工程化之前端工程化詳解 包管理工具

前端工程化詳解 & 包管理工具 前端工程化什么是前端工程化前端工程化發展腳手架能力 體驗度量規范流程效能流程扭轉 穩定性建設針對整體穩定性建設 可監控&#xff1a;前端監控系統 包管理工具npm包詳解package.jsonname 模塊名description 模塊描述信息keywords&#xff1…

《Python實戰進階》No24: PyAutoGUI 實現桌面自動化

No24: PyAutoGUI 實現桌面自動化 摘要 PyAutoGUI 是一個跨平臺的桌面自動化工具&#xff0c;能夠模擬鼠標點擊、鍵盤輸入、屏幕截圖與圖像識別&#xff0c;適用于重復性桌面任務&#xff08;如表單填寫、游戲操作、批量文件處理&#xff09;。本集通過代碼截圖輸出日志的實戰形…

一周學會Flask3 Python Web開發-SQLAlchemy查詢所有數據操作-班級模塊

鋒哥原創的Flask3 Python Web開發 Flask3視頻教程&#xff1a; 2025版 Flask3 Python web開發 視頻教程(無廢話版) 玩命更新中~_嗶哩嗶哩_bilibili 我們來新建一個的藍圖模塊-班級模塊&#xff0c;后面可以和學生模塊&#xff0c;實現一對多的數據庫操作。 blueprint下新建g…

Neural Architecture Search for Transformers:A Survey

摘要 基于 Transformer 的深度神經網絡架構因其在自然語言處理 (NLP) 和計算機視覺 (CV) 領域的各種應用中的有效性而引起了極大的興趣。這些模型是多種語言任務&#xff08;例如情緒分析和文本摘要&#xff09;的實際選擇&#xff0c;取代了長短期記憶 (LSTM) 模型。視覺 Tr…

TCP 全連接隊列 內核層理解socket

TCP 全連接隊列 理解 listen 的第二個參數 int listen(int sockfd, int backlog);backlog 參數表示 全連接隊列&#xff08;accept 隊列&#xff09;的最大長度。 那什么是全連接隊列呢&#xff1f; 三次握手 & accept() 處理流程 客戶端發送 SYN&#xff0c;服務器收到并…

程序化廣告行業(18/89):交易模式與關鍵概念解析

程序化廣告行業&#xff08;18/89&#xff09;&#xff1a;交易模式與關鍵概念解析 大家好呀&#xff01;一直以來&#xff0c;我都在深入研究程序化廣告這個充滿挑戰與機遇的領域&#xff0c;在學習過程中收獲了很多&#xff0c;也迫不及待想和大家分享。寫這篇博客&#xff…

在離線情況下如何使用 Python 翻譯文本

以下是在離線環境下使用Python進行文本翻譯的兩種主流方案&#xff0c;包含本地模型部署和輕量級詞典兩種方法&#xff1a; 方案一&#xff1a;使用本地神經網絡翻譯模型&#xff08;推薦&#xff09; # 安裝依賴&#xff08;需提前下載&#xff09; # pip install argos-tra…

OpenEuler-22.03-LTS上利用Ansible輕松部署MySQL 5.7

一、需求 使用ansible自動化部署mysql二進制部署mysql部署mysql并創建JDBC用戶 二、環境信息 本文涉及的代碼&#xff0c;配置文件地址&#xff1a; 鏈接&#xff1a;百度網盤 請輸入提取碼 提取碼&#xff1a;1g6y 軟件名稱版本備注Ansible2.9.27All modules — Ansible Doc…

基于javaweb的SpringBoot農資商城購物商城系統設計與實現(源碼+文檔+部署講解)

技術范圍&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬蟲、數據可視化、小程序、安卓app、大數據、物聯網、機器學習等設計與開發。 主要內容&#xff1a;免費功能設計、開題報告、任務書、中期檢查PPT、系統功能實現、代碼編寫、論文編寫和輔導、論…