Skip to content

Commit c284e9e

Browse files
committed
优化
1 parent ed66f55 commit c284e9e

4 files changed

Lines changed: 56 additions & 30 deletions

File tree

App.config

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
55
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
11+
</dependentAssembly>
12+
</assemblyBinding>
13+
</runtime>
614
</configuration>

Form1.cs

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
using System.Windows.Forms;
88
using System.IO;
99
using Microsoft.Win32;
10-
using System.Drawing;
11-
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
1210
using System.Linq;
1311
using System.Collections.Generic;
14-
using System.Xml.Linq;
1512
using System.Net.Http;
1613
using Newtonsoft.Json.Linq;
1714

@@ -791,41 +788,49 @@ private async void timer1_Tick(object sender, EventArgs e)
791788

792789
private async Task CheckGitHubVersionAsync()
793790
{
794-
using (HttpClient client = new HttpClient())
791+
try
795792
{
796-
try
793+
// 首先检查是否有网络连接
794+
if (!IsNetworkAvailable())
797795
{
798-
client.DefaultRequestHeaders.UserAgent.ParseAdd("request");
799-
800-
string url = "https://api.github.com/repos/cmliu/CFnat-Windows-GUI/releases/latest";
801-
HttpResponseMessage response = await client.GetAsync(url);
796+
return; // 静默返回,不显示错误
797+
}
802798

803-
if (response.IsSuccessStatusCode)
799+
using (HttpClient client = new HttpClient())
800+
{
801+
try
804802
{
805-
string responseBody = await response.Content.ReadAsStringAsync();
806-
JObject json = JObject.Parse(responseBody);
807-
string latestVersion = json["tag_name"].ToString();
803+
client.DefaultRequestHeaders.UserAgent.ParseAdd("request");
804+
client.Timeout = TimeSpan.FromSeconds(5); // 设置5秒超时
808805

809-
if (latestVersion == 版本号)
810-
{
811-
// 版本相同的逻辑处理
812-
Console.WriteLine("已是最新版本!");
813-
}
814-
else
806+
string url = "https://api.github.com/repos/cmliu/CFnat-Windows-GUI/releases/latest";
807+
HttpResponseMessage response = await client.GetAsync(url);
808+
809+
if (response.IsSuccessStatusCode)
815810
{
816-
// 版本不同的逻辑处理
817-
标题 = "CFnat Windows GUI " + 版本号 + $" 发现新版本: {latestVersion} 请及时更新!";
818-
this.Text = 标题;
819-
Console.WriteLine($"发现新版本: {latestVersion}");
811+
string responseBody = await response.Content.ReadAsStringAsync();
812+
JObject json = JObject.Parse(responseBody);
813+
string latestVersion = json["tag_name"].ToString();
814+
815+
if (latestVersion != 版本号)
816+
{
817+
标题 = "CFnat Windows GUI " + 版本号 + $" 发现新版本: {latestVersion} 请及时更新!";
818+
this.Text = 标题;
819+
}
820820
}
821821
}
822-
}
823-
catch (Exception ex)
824-
{
825-
// 错误处理,比如网络错误等
826-
Console.WriteLine($"Error: {ex.Message}");
822+
catch
823+
{
824+
// 静默处理所有异常(网络错误、超时、JSON解析错误等)
825+
return;
826+
}
827827
}
828828
}
829+
catch
830+
{
831+
// 静默处理任何其他异常
832+
return;
833+
}
829834
}
830835

831836
private void button3_Click(object sender, EventArgs e)
@@ -1045,5 +1050,18 @@ private void button5_Click(object sender, EventArgs e)
10451050
}
10461051
}
10471052

1053+
// 添加检查网络连接的辅助方法
1054+
private bool IsNetworkAvailable()
1055+
{
1056+
try
1057+
{
1058+
return NetworkInterface.GetIsNetworkAvailable();
1059+
}
1060+
catch
1061+
{
1062+
return false; // 如果无法检查网络状态,假设网络不可用
1063+
}
1064+
}
1065+
10481066
}
10491067
}

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ ips-v4.txt # 缓存IPv4库
8484
ips-v6.txt # 缓存IPv6库
8585
locations.json # CF数据中心json文件
8686
Newtonsoft.Json.dll # 验证版本信息组件
87-
Newtonsoft.Json.xml # 验证版本信息组件
8887
```
8988

9089
## Star 星星走起

cfnat.win.gui.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Reference Include="System.ComponentModel.Composition" />
4747
<Reference Include="System.Core" />
4848
<Reference Include="System.Net.Http" />
49+
<Reference Include="System.Numerics" />
4950
<Reference Include="System.Xml.Linq" />
5051
<Reference Include="System.Data.DataSetExtensions" />
5152
<Reference Include="Microsoft.CSharp" />

0 commit comments

Comments
 (0)