tijian_jichuang/Code/SmartUpdater/AppStart.cs
2025-02-20 11:54:48 +08:00

153 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Reflection;
namespace EAS.Distributed
{
static class AppStart
{
#region class SingleInstance
static class SingleInstance
{
private static Mutex mutex = null;
public static bool CreateMutex()
{
return CreateMutex(Assembly.GetEntryAssembly().FullName);
}
public static bool CreateMutex(string name)
{
bool result = false;
mutex = new Mutex(true, name, out result);
return result;
}
public static void ReleaseMutex()
{
if (mutex != null)
{
mutex.Close();
}
}
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
/// <param name="args">控制台参数。</param>
/// <remarks>
/// EAS.SmartUpdater.exe loader=?
/// </remarks>
[STAThread]
static void Main(string[] args)
{
//string dir = Bytes.ToHex(System.Text.Encoding.UTF8.GetBytes("I:\\Temp\\健康体检系统\\publish"));
//string cx = Bytes.ToHex(System.Text.Encoding.UTF8.GetBytes("I:\\Temp\\健康体检系统\\publish\\soh.main.exe"));
//string url = Bytes.ToHex(System.Text.Encoding.UTF8.GetBytes("net.tcp://localhost:6607/eas/services/EAS.UpdateService"));
//只能运动一个升级程序
if (!SingleInstance.CreateMutex("EAS.Distributed"))
{
return;
}
if (args.Length > 0)
{
//1.处理入口参数。
Dictionary<string, string> xList = new Dictionary<string, string>();
foreach (var item in args)
{
try
{
string key = item.Split('=')[0].ToLower();
string value = item.Split('=')[1];
// MessageBox.Show(key + ":" + value);
xList.Add(key, value);
}
catch { }
}
#region //A.引导路径/即引导本程序的程序。
if (xList.ContainsKey("loader"))
{
string value = xList["loader"];
try
{
value = System.Text.UTF8Encoding.UTF8.GetString(Bytes.FromHex(value)); //HEX编码
// MessageBox.Show(value);
}
catch (System.Exception exc)
{
#if DEBUG
using (TextLogger loggger = new TextLogger())
{
loggger.Error(exc);
}
#endif
return;
}
XUpdate.Instance.WorkDirectory = Path.GetDirectoryName(value);
XUpdate.Instance.Application = value;
}
#endregion
#region //C.工作目录。
if (xList.ContainsKey("directory"))
{
string value = xList["directory"];
try
{
XUpdate.Instance.WorkDirectory = System.Text.UTF8Encoding.UTF8.GetString(Bytes.FromHex(value)); //HEX编码
}
catch { }
}
#endregion
#region //C.Url/升级地址。
if (xList.ContainsKey("url"))
{
XUpdate.Instance.AllowUpdate = true;
string value = xList["url"];
try
{
XUpdate.Instance.Url = System.Text.UTF8Encoding.UTF8.GetString(Bytes.FromHex(value)); //HEX编码
}
catch { }
}
#endregion
}
//记录3个参数。
#if DEBUG
using (TextLogger logger = new TextLogger())
{
logger.Info(string.Format("directory={0}", XUpdate.Instance.WorkDirectory));
logger.Info(string.Format("loader={0}", XUpdate.Instance.Application));
logger.Info(string.Format("url={0}", XUpdate.Instance.Url));
}
#endif
//2.启动程序。
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new UpdateUI1());
}
}
}