old_flsystem/PCRobot/Program.cs

216 lines
9.7 KiB
C#
Raw Normal View History

2022-09-20 03:10:29 +00:00
using CsharpHttpHelper;
using PCRobot.Utils;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WechatHelper;
2022-09-20 03:10:29 +00:00
namespace PCRobot
{
static class Program
{
static void WriteResource()
{
//Util.WriteResource(HttpExtend.MapFile("WeChatHelper_3.3.0.93.dll", "Bin"), HttpExtend.MapFile("WeChatHelper_3.3.0.93.dll", @"TempFile"));
Util.WriteResource(HttpExtend.MapFile("WeChatHelper_3.4.0.38.dll", "Bin"), HttpExtend.MapFile("WeChatHelper_3.4.0.38.dll", @"TempFile"), "3.4.0.38");
Util.WriteResource(HttpExtend.MapFile("WeChatServer.dll", "Bin"), HttpExtend.MapFile("WeChatServer.dll", @"TempFile"), "3.4.0.38");
//Util.WriteResource(HttpExtend.MapFile("NanMuCore_3.2.1.154.dll", "Bin"),HttpExtend.MapFile("NanMuCore_3.2.1.154.dll", @"TempFile"));
//Util.WriteResource(HttpExtend.MapFile("NanMuCore_3.3.5.50.dll", "Bin"), HttpExtend.MapFile("NanMuCore_3.3.5.50.dll", @"TempFile"));
Util.WriteResource(HttpExtend.MapFile("WxWorkHelper_4.0.2.6026.dll", "Bin"), HttpExtend.MapFile("WxWorkHelper_4.0.2.6026.dll", @"TempFile"), "3.1.10.3010", true);
Util.WriteResource(HttpExtend.MapFile("WxWorkLoader.dll", "Bin"), HttpExtend.MapFile("WxWorkLoader.dll", @"TempFile"), "3.1.10.3010", true);
//Util.WriteResource(HttpExtend.MapFile("WxWorkHelper_3.1.10.3010.dll", "Bin"), HttpExtend.MapFile("WxWorkHelper_3.1.10.3010.dll", @"TempFile"), "3.1.10.3010");
//Util.WriteResource(HttpExtend.MapFile("WxWorkHelper_3.0.27.2701.dll", "Bin"), HttpExtend.MapFile("WxWorkHelper_3.0.27.2701.dll", @"TempFile"));
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
try
{
string strFullPath = Application.ExecutablePath;
string strFileName = Path.GetFileName(strFullPath);
string curPath = HttpExtend.MapFile(strFileName).ToLower();
int count = 0;
foreach (Process p in Process.GetProcessesByName(strFileName.Replace(".exe", "")))
{
string path = p.MainModule.FileName.ToLower();
if (path == curPath) count++;
}
if (count > 1)
{
MessageBox.Show("抱歉,本程序禁止多开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);//弹出提示信息
return;
}
try
{
WriteResource();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "请关闭微信后重试", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//new LevelUpdate.UpdateForm().ShowDialog();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
SetAllowUnsafeHeaderParsing20(true);
try
{
var LastData = UpdateClient.GetCloudFileHist();
if (!string.IsNullOrWhiteSpace(LastData))
{
if (new UpdateForm(LastData).ShowDialog() == DialogResult.OK)
{
Process.Start("检查更新.exe", "易转发.exe");
return;
}
}
}
catch (Exception ex)
{ }
Application.Run(new PCRobotForm());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "#-#" + ex.StackTrace);
}
}
#region
static bool SetAllowUnsafeHeaderParsing20(bool useUnsafe)
{
try
{
//Get the assembly that contains the internal class
System.Reflection.Assembly aNetAssembly = System.Reflection.Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
if (aNetAssembly != null)
{
//Use the assembly in order to get the internal type for the internal class
Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
if (aSettingsType != null)
{
//Use the internal static property to get an instance of the internal settings class.
//If the static instance isn't created allready the property will create it for us.
object anInstance = aSettingsType.InvokeMember("Section",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.NonPublic, null, null, new object[] { });
if (anInstance != null)
{
//Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
System.Reflection.FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (aUseUnsafeHeaderParsing != null)
{
aUseUnsafeHeaderParsing.SetValue(anInstance, useUnsafe);
return true;
}
}
}
}
return false;
}
catch (Exception ex)
{
LogHelper.GetSingleObj().Error("系统异常捕获", $"系统异常捕获:{ex.Message} - {ex.StackTrace}");
MessageBox.Show(ex.Message + " - " + ex.StackTrace);
}
return false;
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
try
{
string str = GetExceptionMsg(e.Exception, e.ToString());
//MessageBox.Show(str, "子线程错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new Exception(str);
}
catch (Exception ex)
{
LogHelper.GetSingleObj().Error("子线程错误", $"子线程错误:{ex.Message} - {ex.StackTrace}");
MessageBox.Show(ex.Message, "子线程错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
// MessageBox.Show(str, "主线程错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new Exception(str);
}
catch (Exception ex)
{
LogHelper.GetSingleObj().Error("主线程错误", $"主线程错误:{ex.Message} - {ex.StackTrace}");
MessageBox.Show(ex.Message, "主线程错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************异常文本****************************");
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
/// <summary>
/// 退出
/// </summary>
public static void Exit(bool start = false)
{
try
{
if (start)
{
//程序位置
string strAppFileName = Process.GetCurrentProcess().MainModule.FileName;
Process myNewProcess = new Process();
//要启动的应用程序
myNewProcess.StartInfo.FileName = strAppFileName;
// 设置要启动的进程的初始目录
myNewProcess.StartInfo.WorkingDirectory = Application.ExecutablePath;
//启动程序
myNewProcess.Start();
}
//结束该程序
// Application.Exit();
//结束该所有线程
//Environment.Exit(0);
LogHelper.GetSingleObj().Info("调用Exit退出", $"调用Exit退出程序");
Thread.Sleep(1000);
Process.GetCurrentProcess().Kill();
}
catch (Exception)
{ }
}
#endregion
}
}