这篇文章主要讲解了“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() + "<br>");
}
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]);
}
}
运行后的截图
感谢各位的阅读,以上就是“c#怎么实现定时自动获取免费natapp的动态域名和端口”的内容了,经过本文的学习后,相信大家对c#怎么实现定时自动获取免费natapp的动态域名和端口这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。