diff --git a/shadowsocks-csharp/Controller/FileManager.cs b/shadowsocks-csharp/Controller/FileManager.cs index f15a1cc1..7769be43 100755 --- a/shadowsocks-csharp/Controller/FileManager.cs +++ b/shadowsocks-csharp/Controller/FileManager.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.IO.Compression; +using System.Text; namespace Shadowsocks.Controller { @@ -39,5 +40,19 @@ namespace Shadowsocks.Controller } } } + + public static string NonExclusiveReadAllText(string path) + { + return NonExclusiveReadAllText(path, Encoding.Default); + } + + public static string NonExclusiveReadAllText(string path, Encoding encoding) + { + using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + using (var sr = new StreamReader(fs, encoding)) + { + return sr.ReadToEnd(); + } + } } } diff --git a/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs b/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs index 3d04fafc..30cd7d34 100644 --- a/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs +++ b/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs @@ -39,7 +39,7 @@ namespace Shadowsocks.Controller List lines = ParseResult(e.Result); if (File.Exists(PACServer.USER_RULE_FILE)) { - string local = File.ReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); + string local = FileManager.NonExclusiveReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); using (var sr = new StringReader(local)) { foreach (var rule in sr.NonWhiteSpaceLines()) @@ -53,7 +53,7 @@ namespace Shadowsocks.Controller string abpContent; if (File.Exists(PACServer.USER_ABP_FILE)) { - abpContent = File.ReadAllText(PACServer.USER_ABP_FILE, Encoding.UTF8); + abpContent = FileManager.NonExclusiveReadAllText(PACServer.USER_ABP_FILE, Encoding.UTF8); } else { @@ -62,7 +62,7 @@ namespace Shadowsocks.Controller abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented)); if (File.Exists(PACServer.PAC_FILE)) { - string original = File.ReadAllText(PACServer.PAC_FILE, Encoding.UTF8); + string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8); if (original == abpContent) { UpdateCompleted(this, new ResultEventArgs(false)); diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index c287bd29..9ec544d9 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -557,10 +557,10 @@ namespace Shadowsocks.Controller UpdatePACFromGFWList(); return; } - List lines = GFWListUpdater.ParseResult(File.ReadAllText(Utils.GetTempPath("gfwlist.txt"))); + List lines = GFWListUpdater.ParseResult(FileManager.NonExclusiveReadAllText(Utils.GetTempPath("gfwlist.txt"))); if (File.Exists(PACServer.USER_RULE_FILE)) { - string local = File.ReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); + string local = FileManager.NonExclusiveReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); using (var sr = new StringReader(local)) { foreach (var rule in sr.NonWhiteSpaceLines()) @@ -574,7 +574,7 @@ namespace Shadowsocks.Controller string abpContent; if (File.Exists(PACServer.USER_ABP_FILE)) { - abpContent = File.ReadAllText(PACServer.USER_ABP_FILE, Encoding.UTF8); + abpContent = FileManager.NonExclusiveReadAllText(PACServer.USER_ABP_FILE, Encoding.UTF8); } else { @@ -583,7 +583,7 @@ namespace Shadowsocks.Controller abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented)); if (File.Exists(PACServer.PAC_FILE)) { - string original = File.ReadAllText(PACServer.PAC_FILE, Encoding.UTF8); + string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8); if (original == abpContent) { return;