Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

C # how to automatically obtain the dynamic domain name and port of free natapp on a regular basis

Shulou Source: shulou.com Published: 2022-06-02 04:36:03 09月16日 Update

这篇文章主要讲解了"c#怎么实现定时自动获取免费natapp的动态域名和端口",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"c#怎么实现定时自动获取免费natapp的动态域名和端口"吧!

一、关闭natapp.exe进程

KillProcess("natapp"); private static void KillProcess(string processName) { Process[] myproc = Process.GetProcesses(); foreach (Process item in myproc) { if (item.ProcessName == processName) { item.Kill(); } } }

二、删除natapp.exe运行产生的log文件

DeleteLog("log.txt");

private static void DeleteLog(string logPath) { DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory); FileInfo[] fis = di.GetFiles(); foreach (FileInfo fi in fis) { try { if (fi.Name.Contains(logPath)) { fi.Delete(); } } catch (Exception ex) { } } }

三、启动natapp.exe进程

StartNatappProcess(Environment.CurrentDirectory + "\\natapp\\natapp.exe");

private static void StartNatappProcess(string natappPath) { Process process = new Process(); process.StartInfo.FileName = natappPath; //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // 启动的时候最小化程序 process.Start(); Thread.Sleep(1000); }

四、解析natapp.exe运行产生的log文件

ResolveLog("log.txt");

private static void ResolveLog(string logPath) { resolveResult = new ResolveResult(); using (FileStream fs = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (StreamReader sr = new StreamReader(fs, Encoding.Default)) { StringBuilder sb = new StringBuilder(); while (!sr.EndOfStream) { sb.AppendLine(sr.ReadLine() + "

"); } string str = sb.ToString(); int x = str.IndexOf("Url"); //定位位置 string validData = str.Substring(x, 100); string[] strTmps = validData.Split('"'); string[] serverStr = strTmps[2].Split(':'); resolveResult.serverUrl = serverStr[0] + ":" + serverStr[1]; if (serverStr.Length>2) { resolveResult.serverPort = int.Parse(serverStr[2]); } resolveResult.localIP = strTmps[10].Split(':')[0]; resolveResult.localPort = int.Parse(strTmps[10].Split(':')[1]); } }

运行后的截图

Thank you for reading, the above is the content of "c#how to automatically obtain the dynamic domain name and port of free natapp on a regular basis", after learning this article, I believe everyone has a deeper understanding of how to automatically obtain the dynamic domain name and port of free natapp on a regular basis. The specific use situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Dynamic domain name port caching learning running content file process minimum location that is ideas situations screenshots articles time more knowledge knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB macOS MySQL Shulou Technology