Move Updater into Main Program

This commit is contained in:
ChsBuffer
2021-02-14 13:19:58 +08:00
parent f728759bb2
commit faa3cf3770
17 changed files with 257 additions and 770 deletions

View File

@@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Netch", "Netch\Netch.csproj
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetchLib", "NetchLib\NetchLib.csproj", "{A8715AF4-ACC6-43F9-9381-4294C5360623}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetchUpdater", "NetchUpdater\NetchUpdater.csproj", "{828318A8-9B90-4A5F-BD6B-E632CC9D8933}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj", "{53397641-35CA-4336-8E22-2CE12EF476AC}"
EndProject
Global
@@ -25,10 +23,6 @@ Global
{A8715AF4-ACC6-43F9-9381-4294C5360623}.Debug|x64.Build.0 = Debug|x64
{A8715AF4-ACC6-43F9-9381-4294C5360623}.Release|x64.ActiveCfg = Release|x64
{A8715AF4-ACC6-43F9-9381-4294C5360623}.Release|x64.Build.0 = Release|x64
{828318A8-9B90-4A5F-BD6B-E632CC9D8933}.Debug|x64.ActiveCfg = Debug|x64
{828318A8-9B90-4A5F-BD6B-E632CC9D8933}.Debug|x64.Build.0 = Debug|x64
{828318A8-9B90-4A5F-BD6B-E632CC9D8933}.Release|x64.ActiveCfg = Release|x64
{828318A8-9B90-4A5F-BD6B-E632CC9D8933}.Release|x64.Build.0 = Release|x64
{53397641-35CA-4336-8E22-2CE12EF476AC}.Debug|x64.ActiveCfg = Debug|x64
{53397641-35CA-4336-8E22-2CE12EF476AC}.Debug|x64.Build.0 = Debug|x64
{53397641-35CA-4336-8E22-2CE12EF476AC}.Release|x64.ActiveCfg = Release|x64

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
@@ -8,6 +7,7 @@ using System.Threading.Tasks;
using Netch.Models.GitHubRelease;
using Netch.Utils;
using Newtonsoft.Json;
using static Netch.Updater.Updater;
namespace Netch.Controllers
{
@@ -19,7 +19,7 @@ namespace Netch.Controllers
public const string Name = @"Netch";
public const string Copyright = @"Copyright © 2019 - 2021";
public const string AssemblyVersion = @"1.7.4";
public const string AssemblyVersion = @"1.7.3";
private const string Suffix = @"";
public static readonly string Version = $"{AssemblyVersion}{(string.IsNullOrEmpty(Suffix) ? "" : $"-{Suffix}")}";
@@ -68,7 +68,7 @@ namespace Netch.Controllers
}
}
public static async Task UpdateNetch(DownloadProgressChangedEventHandler onDownloadProgressChanged)
public static async Task DownloadUpdate(DownloadProgressChangedEventHandler onDownloadProgressChanged)
{
using WebClient client = new();
@@ -87,7 +87,7 @@ namespace Netch.Controllers
{
if (Utils.Utils.SHA256CheckSum(fileFullPath) == sha256)
{
RunUpdater();
UpdateNetch(fileFullPath);
return;
}
@@ -108,21 +108,7 @@ namespace Netch.Controllers
if (Utils.Utils.SHA256CheckSum(fileFullPath) != sha256)
throw new Exception(i18N.Translate("The downloaded file has the wrong hash"));
RunUpdater();
void RunUpdater()
{
// if debugging process stopped, debugger will kill child processes!!!!
// 调试进程结束,调试器将会杀死子进程
// uncomment if(!Debugger.isAttach) block in NetchUpdater Project's main() method and attach to NetchUpdater process to debug
// 在 NetchUpdater 项目的 main() 方法中取消注释 if!Debugger.isAttach并附加到 NetchUpdater 进程进行调试
Process.Start(new ProcessStartInfo
{
FileName = Path.Combine(Global.NetchDir, "NetchUpdater.exe"),
Arguments =
$"{Global.Settings.UDPSocketPort} \"{fileFullPath}\" \"{Global.NetchDir}\""
});
}
UpdateNetch(fileFullPath);
}
}
}

View File

@@ -1346,7 +1346,7 @@ namespace Netch.Forms
Hide();
}
private async void Exit(bool forceExit = false)
public async void Exit(bool forceExit = false)
{
if (!IsWaiting() && !Global.Settings.StopWhenExited && !forceExit)
{
@@ -1441,7 +1441,7 @@ namespace Netch.Forms
BeginInvoke(new Action(() => { NewVersionLabel.Text = $"{args.ProgressPercentage}%"; }));
}
await UpdateChecker.UpdateNetch(OnDownloadProgressChanged);
await UpdateChecker.DownloadUpdate(OnDownloadProgressChanged);
}
catch (Exception exception)
{

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
@@ -69,43 +68,11 @@ namespace Netch.Forms.Mode
{
try
{
var RDirInfo = new DirectoryInfo(DirName);
if (!RDirInfo.Exists)
{
return;
}
RuleListBox.Items.AddRange(Directory.GetFiles(DirName, "*.exe", SearchOption.AllDirectories));
}
catch (Exception)
catch (Exception e)
{
return;
}
var DirStack = new Stack<string>();
DirStack.Push(DirName);
while (DirStack.Count > 0)
{
var DirInfo = new DirectoryInfo(DirStack.Pop());
try
{
foreach (var DirChildInfo in DirInfo.GetDirectories())
{
DirStack.Push(DirChildInfo.FullName);
}
foreach (var FileChildInfo in DirInfo.GetFiles())
{
if (FileChildInfo.Name.EndsWith(".exe") && !RuleListBox.Items.Contains(FileChildInfo.Name))
{
RuleListBox.Items.Add(FileChildInfo.Name);
Edited = true;
}
}
}
catch (Exception)
{
// ignored
}
// ignored
}
}
@@ -216,7 +183,8 @@ namespace Netch.Forms.Mode
}
else
{
var fullName = ModeHelper.GetFullPath(FilenameTextBox.Text + ".txt");
var relativePath = $"Custom\\{FilenameTextBox.Text}.txt";
var fullName = ModeHelper.GetFullPath(relativePath);
if (File.Exists(fullName))
{
MessageBoxX.Show(i18N.Translate("File already exists.\n Please Change the filename"));
@@ -227,6 +195,7 @@ namespace Netch.Forms.Mode
{
BypassChina = false,
FileName = FilenameTextBox.Text,
RelativePath = relativePath,
Type = 0,
Remark = RemarkTextBox.Text
};

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using WindowsJobAPI;
@@ -26,6 +27,8 @@ namespace Netch
/// </summary>
public static MainForm MainForm;
public static readonly Mutex Mutex = new(false, "Global\\Netch");
public static class Flags
{
public static bool SupportFakeDns => _supportFakeDns ??= new TUNTAPController().TestFakeDNS();

View File

@@ -22,69 +22,67 @@ namespace Netch
if (!NativeMethods.AttachConsole(-1))
NativeMethods.AllocConsole();
// 创建互斥体防止多次运行
using (var mutex = new Mutex(false, "Global\\Netch"))
// 设置当前目录
Directory.SetCurrentDirectory(Global.NetchDir);
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process) + ";" + Path.Combine(Global.NetchDir, "bin"), EnvironmentVariableTarget.Process);
Updater.Updater.CleanOld();
// 预创建目录
var directories = new[] {"mode\\Custom", "data", "i18n", "logging"};
foreach (var item in directories)
if (!Directory.Exists(item))
Directory.CreateDirectory(item);
// 加载配置
Configuration.Load();
// 加载语言
i18N.Load(Global.Settings.Language);
if (!Directory.Exists("bin") || !Directory.EnumerateFileSystemEntries("bin").Any())
{
// 设置当前目录
Directory.SetCurrentDirectory(Global.NetchDir);
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process) + ";" + Path.Combine(Global.NetchDir, "bin"), EnvironmentVariableTarget.Process);
// 预创建目录
var directories = new[] {"mode", "data", "i18n", "logging"};
foreach (var item in directories)
if (!Directory.Exists(item))
Directory.CreateDirectory(item);
// 加载配置
Configuration.Load();
// 加载语言
i18N.Load(Global.Settings.Language);
if (!Directory.Exists("bin") || !Directory.EnumerateFileSystemEntries("bin").Any())
{
MessageBoxX.Show(i18N.Translate("Please extract all files then run the program!"));
Environment.Exit(2);
}
// 检查是否已经运行
if (!mutex.WaitOne(0, false))
{
OnlyInstance.Send(OnlyInstance.Commands.Show);
Logging.Info("唤起单实例");
// 退出进程
Environment.Exit(1);
}
// 清理上一次的日志文件,防止淤积占用磁盘空间
if (Directory.Exists("logging"))
{
var directory = new DirectoryInfo("logging");
foreach (var file in directory.GetFiles())
file.Delete();
foreach (var dir in directory.GetDirectories())
dir.Delete(true);
}
Logging.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}");
Task.Run(() => { Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Application.ExecutablePath)}"); });
Task.Run(() =>
{
Logging.Info("启动单实例");
OnlyInstance.Server();
});
// 绑定错误捕获
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_OnException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(Global.MainForm = new MainForm());
MessageBoxX.Show(i18N.Translate("Please extract all files then run the program!"));
Environment.Exit(2);
}
// 检查是否已经运行
if (!Global.Mutex.WaitOne(0, false))
{
OnlyInstance.Send(OnlyInstance.Commands.Show);
Logging.Info("唤起单实例");
// 退出进程
Environment.Exit(1);
}
// 清理上一次的日志文件,防止淤积占用磁盘空间
if (Directory.Exists("logging"))
{
var directory = new DirectoryInfo("logging");
foreach (var file in directory.GetFiles())
file.Delete();
foreach (var dir in directory.GetDirectories())
dir.Delete(true);
}
Logging.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}");
Task.Run(() => { Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Application.ExecutablePath)}"); });
Task.Run(() =>
{
Logging.Info("启动单实例");
OnlyInstance.Server();
});
// 绑定错误捕获
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_OnException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(Global.MainForm = new MainForm());
}
public static void Application_OnException(object sender, ThreadExceptionEventArgs e)

View File

@@ -65,7 +65,6 @@
<ItemGroup>
<ProjectReference Include="..\NetchLib\NetchLib.csproj" />
<ProjectReference Include="..\NetchUpdater\NetchUpdater.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -149,5 +149,15 @@ namespace Netch.Properties {
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] _7za {
get {
object obj = ResourceManager.GetObject("7za", resourceCulture);
return ((byte[])(obj));
}
}
}
}

View File

@@ -169,4 +169,9 @@
PublicKeyToken=b03f5f7f11d50a3a
</value>
</data>
<data name="7za" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\7za.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</data>
</root>

167
Netch/Updater/Updater.cs Normal file
View File

@@ -0,0 +1,167 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Windows.Forms;
using Netch.Controllers;
using Netch.Forms;
using Netch.Properties;
using Netch.Utils;
namespace Netch.Updater
{
public static class Updater
{
public static void UpdateNetch(string updateFilePath)
{
var tempFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var extractPath = Path.Combine(tempFolder, "extract");
int exitCode;
if ((exitCode = Extract(updateFilePath, extractPath, true, tempFolder)) != 0)
{
MessageBoxX.Show($"7za exit with code {exitCode}");
return;
}
foreach (var file in Directory.GetFiles(Global.NetchDir, "*", SearchOption.AllDirectories))
{
if (new[] {"data", "mode\\Custom"}.ToList().Any(p => file.StartsWith(Path.Combine(Global.NetchDir, p))))
continue;
try
{
File.Move(file, file + ".old");
}
catch
{
throw new Exception("Updater wasn't able to rename file: " + file);
}
}
MoveDirectory(Path.Combine(extractPath, "Netch"), Global.NetchDir, true);
Global.Mutex.ReleaseMutex();
Process.Start(Application.ExecutablePath);
Global.MainForm.Exit(true);
}
private static int Extract(string archiveFileName, string destDirName, bool overwrite, string tempFolder = null)
{
tempFolder ??= Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var temp7za = Path.Combine(tempFolder, "7za.exe");
archiveFileName = Path.GetFullPath(archiveFileName);
destDirName = Path.GetFullPath(destDirName);
if (!Directory.Exists(tempFolder))
Directory.CreateDirectory(tempFolder);
if (!File.Exists(temp7za))
File.WriteAllBytes(temp7za, Resources._7za);
var argument = new StringBuilder();
argument.Append($" x \"{archiveFileName}\" -o\"{destDirName}\" ");
if (overwrite)
argument.Append(" -y ");
var process = Process.Start(new ProcessStartInfo
{
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = temp7za,
Arguments = argument.ToString()
});
process?.WaitForExit();
return process?.ExitCode ?? 2;
}
public static FileInfo FindFile(string filename, string directory)
{
var DirStack = new Stack<string>();
DirStack.Push(directory);
while (DirStack.Count > 0)
{
var DirInfo = new DirectoryInfo(DirStack.Pop());
try
{
foreach (var DirChildInfo in DirInfo.GetDirectories())
{
DirStack.Push(DirChildInfo.FullName);
}
}
catch
{
// ignored
}
try
{
foreach (var FileChildInfo in DirInfo.GetFiles())
{
if (FileChildInfo.Name == filename)
{
return FileChildInfo;
}
}
}
catch
{
// ignored
}
}
return null;
}
private static void MoveDirectory(string sourceDirName, string destDirName, bool overwrite)
{
sourceDirName = Path.GetFullPath(sourceDirName);
destDirName = Path.GetFullPath(destDirName);
if (!overwrite)
{
Directory.Move(sourceDirName, destDirName);
}
else
{
foreach (var dir in Directory.GetDirectories(sourceDirName, "*", SearchOption.AllDirectories))
{
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
}
foreach (var f in Directory.GetFiles(sourceDirName, "*", SearchOption.AllDirectories))
{
try
{
File.Move(f, f.Replace(sourceDirName, destDirName));
}
catch (Exception e)
{
throw new Exception("Updater wasn't able to move file: " + f);
}
}
}
}
private static bool TestFileFree(string FileName)
{
try
{
File.Move(FileName, FileName);
return true;
}
catch
{
return false;
}
}
public static void CleanOld()
{
foreach (var f in Directory.GetFiles(Global.NetchDir, "*.old", SearchOption.AllDirectories))
File.Delete(f);
}
}
}

View File

@@ -1,2 +0,0 @@
/bin
/obj

View File

@@ -1,59 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<StartupObject>NetchUpdater.Program</StartupObject>
<Platforms>x64</Platforms>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<LangVersion>latest</LangVersion>
<PackageProjectUrl>https://github.com/NetchX/Netch</PackageProjectUrl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsAsErrors />
<NoWarn />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn />
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsAsErrors />
<OutputPath>bin\x64\Release\</OutputPath>
</PropertyGroup>
<ItemGroup>
<None Remove=".gitignore" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
</Project>

View File

@@ -1,351 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using NetchUpdater.Properties;
namespace NetchUpdater
{
internal class Program
{
private static readonly string UpdaterFullName;
private static readonly string UpdaterDirectory;
private static readonly string UpdaterFriendlyName;
private static readonly Process CurrentProcess;
static Program()
{
CurrentProcess = Process.GetCurrentProcess();
UpdaterFullName = CurrentProcess.MainModule.FileName;
UpdaterDirectory = Path.GetDirectoryName(UpdaterFullName);
UpdaterFriendlyName = Path.GetFileName(UpdaterFullName);
}
public static void Main(string[] args)
{
var result = false;
try
{
#region Check Arguments
if (CurrentProcess.MainModule == null)
{
Console.WriteLine("Current Process MainModule is null");
return;
}
if (args.Length != 3)
{
Console.WriteLine("The program is not user-oriented\n此程序不是面向用户的");
return;
}
// arg0 port
if (!int.TryParse(args[0], out var port))
{
Console.WriteLine("arg0 Port Parse failed");
return;
}
var updateExtracted = true;
// arg1 update File/Directory
var updatePath = Path.GetFullPath(args[1]);
if (File.Exists(updatePath))
{
updateExtracted = false;
}
else if (!Directory.Exists(updatePath))
{
Console.WriteLine("arg1 update file/directory Not found");
return;
}
// arg2 target Directory
string targetPath;
if (!File.Exists(Path.Combine(targetPath = Path.GetFullPath(args[2]), "Netch.exe")))
{
Console.Write("arg2 Netch Directory doesn't seems right");
return;
}
#region if under target Directory,then rerun in temp directory
if (UpdaterDirectory.StartsWith(targetPath))
{
// Updater 在目标目录下
// 将程序复制到临时目录,传递参数
var tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var newUpdaterPath = Path.Combine(tempPath, UpdaterFriendlyName);
Directory.CreateDirectory(tempPath);
File.Copy(UpdaterFullName, newUpdaterPath);
Process.Start(new ProcessStartInfo
{
FileName = newUpdaterPath,
Arguments = $"{port} \"{updatePath}\" \"{targetPath}\"",
WorkingDirectory = tempPath,
UseShellExecute = false
});
result = true;
return;
}
#endregion
#endregion
/*Console.WriteLine("Waiting Attach");
while (!Debugger.IsAttached)
{
Thread.Sleep(1000);
}*/
#region Send Netch Exit command
Process[] _;
if ((_ = Process.GetProcessesByName("Netch")).Any())
{
Console.WriteLine("Found Netch process, Send exit command");
try
{
var udpClient = new UdpClient("127.0.0.1", port);
var sendBytes = Encoding.ASCII.GetBytes("Exit");
udpClient.Send(sendBytes, sendBytes.Length);
}
catch
{
Console.WriteLine("Send command failed");
return;
}
foreach (var proc in _)
{
try
{
proc.WaitForExit();
}
catch (Exception)
{
// ignored
}
}
}
#endregion
var counter = 0;
while (!TestFileFree(Path.Combine(targetPath, "Netch.exe")))
{
// wait 5 sec
if (counter > 25)
{
Console.WriteLine("Waiting Netch exit timeout");
return;
}
Thread.Sleep(200);
counter++;
}
#region Update
string extractPath = null;
if (!updateExtracted)
{
extractPath = Path.Combine(UpdaterDirectory, "extract");
Extract(updatePath, extractPath, true);
var netchExeFileInfo = FindFile("Netch.exe", extractPath);
if (netchExeFileInfo == null)
{
throw new Exception("Netch.exe not found in archive!");
}
updatePath = netchExeFileInfo.Directory.FullName;
}
MoveDirectory(updatePath, targetPath, true);
try
{
if (extractPath != null)
Directory.Delete(extractPath, true);
}
catch
{
// ignored
}
#endregion
#region Finished Update,Start Netch
Console.WriteLine("Start Netch");
Process.Start(new ProcessStartInfo
{
FileName = Path.Combine(targetPath, "Netch.exe"),
UseShellExecute = true,
});
#endregion
result = true;
}
catch (Exception e)
{
if (e is InvalidDataException)
Console.WriteLine("Archive file Broken");
Console.WriteLine(e.ToString());
}
finally
{
if (!result)
{
Console.WriteLine("Press any key to exit...");
Console.Read();
}
}
}
private static void Extract(string archiveFileName, string destDirName, bool overwrite)
{
archiveFileName = Path.GetFullPath(archiveFileName);
destDirName = Path.GetFullPath(destDirName);
if (!File.Exists("7za.exe"))
File.WriteAllBytes("7za.exe", Resources._7za);
var argument = new StringBuilder();
argument.Append($" x \"{archiveFileName}\" -o\"{destDirName}\" ");
if (overwrite)
argument.Append(" -y ");
Process.Start(new ProcessStartInfo
{
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = Path.GetFullPath("7za.exe"),
Arguments = argument.ToString()
})?.WaitForExit();
}
public static FileInfo FindFile(string filename, string directory)
{
var DirStack = new Stack<string>();
DirStack.Push(directory);
while (DirStack.Count > 0)
{
var DirInfo = new DirectoryInfo(DirStack.Pop());
try
{
foreach (var DirChildInfo in DirInfo.GetDirectories())
{
DirStack.Push(DirChildInfo.FullName);
}
}
catch
{
// ignored
}
try
{
foreach (var FileChildInfo in DirInfo.GetFiles())
{
if (FileChildInfo.Name == filename)
{
return FileChildInfo;
}
}
}
catch
{
// ignored
}
}
return null;
}
private static void MoveDirectory(string sourceDirName, string destDirName, bool overwrite)
{
sourceDirName = Path.GetFullPath(sourceDirName);
destDirName = Path.GetFullPath(destDirName);
if (!overwrite)
{
Directory.Move(sourceDirName, destDirName);
}
else
{
var dirStack = new Stack<string>();
dirStack.Push(sourceDirName);
while (dirStack.Count > 0)
{
var dirInfo = new DirectoryInfo(dirStack.Pop());
try
{
foreach (var dirChildInfo in dirInfo.GetDirectories())
{
var destPath = dirChildInfo.FullName.Replace(sourceDirName, destDirName);
try
{
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
}
catch (Exception e)
{
Console.WriteLine($"Create {destPath} Folder Error: {e.Message}");
}
dirStack.Push(dirChildInfo.FullName);
}
foreach (var fileChildInfo in dirInfo.GetFiles())
{
var destPath = fileChildInfo.FullName.Replace(sourceDirName, destDirName);
try
{
if (File.Exists(destPath))
{
File.Delete(destPath);
}
fileChildInfo.MoveTo(destPath);
}
catch (Exception e)
{
Console.WriteLine($"Move {fileChildInfo.FullName} To {destPath} Error: {e.Message}");
}
}
}
catch (Exception e)
{
Console.WriteLine($"ERROR:{e.Message}");
}
}
}
}
private static bool TestFileFree(string FileName)
{
try
{
File.Move(FileName, FileName);
return true;
}
catch
{
return false;
}
}
}
}

View File

@@ -1,35 +0,0 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("NetchUpdater")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NetchUpdater")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("828318A8-9B90-4A5F-BD6B-E632CC9D8933")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -1,73 +0,0 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace NetchUpdater.Properties {
using System;
/// <summary>
/// 一个强类型的资源类,用于查找本地化的字符串等。
/// </summary>
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// 返回此类使用的缓存的 ResourceManager 实例。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("NetchUpdater.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 重写当前线程的 CurrentUICulture 属性
/// 重写当前线程的 CurrentUICulture 属性。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// 查找 System.Byte[] 类型的本地化资源。
/// </summary>
internal static byte[] _7za {
get {
object obj = ResourceManager.GetObject("7za", resourceCulture);
return ((byte[])(obj));
}
}
}
}

View File

@@ -1,124 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="7za" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\7za.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>

Binary file not shown.