新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要讲解了“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]);
}
}
运行后的截图
感谢各位的阅读,以上就是“c#怎么实现定时自动获取免费natapp的动态域名和端口”的内容了,经过本文的学习后,相信大家对c#怎么实现定时自动获取免费natapp的动态域名和端口这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!