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

268 lines
7.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using EAS.Distributed;
using System.ServiceModel.Channels;
using System.ServiceModel;
namespace EAS.Distributed
{
class XUpdate
{
string allowUpdate = "0";
string url = string.Empty;
string m_Application = "EAS.WinClient.Start.exe";
string smartConfigFile = string.Empty;
string m_Title = "天瑞体检自动更新程序";
string m_StartPath = string.Empty;
IUpdateService service = null;
#region
static XUpdate instance = new XUpdate();
static readonly object _lock = new object();
/// <summary>
/// 单例。
/// </summary>
public static XUpdate Instance
{
get
{
lock (_lock)
{
if (instance == null)
{
instance = new XUpdate();
}
}
return instance;
}
}
internal XUpdate()
{
string file = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "update.ini");
IniFile iniFile = new IniFile(file);
//1.基本参数
try
{
this.allowUpdate = iniFile.ReadValue("config", "allow");
}
catch
{
this.allowUpdate = "0";
}
try
{
this.url = iniFile.ReadValue("config", "url");
}
catch
{
this.url = string.Empty;
}
this.WorkDirectory = System.Windows.Forms.Application.StartupPath;
//2.是否读配置文件中的启动文件。
try
{
this.m_Application = iniFile.ReadValue("config", "application");
}
catch
{
}
//包括路径。
this.m_Application = Path.Combine(this.WorkDirectory, this.m_Application);
try
{
this.m_Title = iniFile.ReadValue("config", "title");
}
catch
{
}
}
#endregion
public IUpdateService Service
{
get
{
return service;
}
}
/// <summary>
/// 充许升级。
/// </summary>
public bool AllowUpdate
{
get
{
return this.allowUpdate =="1";
}
set
{
this.allowUpdate = value ? "1" :"0";
}
}
/// <summary>
/// 智能服务地址。
/// </summary>
public string Url
{
get
{
return this.url;
}
set
{
this.url = value;
}
}
/// <summary>
/// 主界面标题。
/// </summary>
public string Title
{
get
{
return this.m_Title;
}
set
{
this.m_Title = value;
}
}
/// <summary>
/// 工作目录。
/// </summary>
public string WorkDirectory
{
get;
set;
}
/// <summary>
/// 主程序名称(EXE)。
/// </summary>
public string Application
{
get
{
return this.m_Application;
}
set
{
this.m_Application = value;
}
}
/// <summary>
/// 应用基础目录。
/// </summary>
/// <returns></returns>
public string GetBaseDirectory()
{
if (this.WorkDirectory.Length > 0)
return this.WorkDirectory;
else
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}
/// <summary>
/// 升级临时目录。
/// </summary>
/// <returns></returns>
public string GetDownDirectory()
{
return Path.Combine(GetBaseDirectory(), "t-update");
}
internal static void GetService()
{
string url = Instance.url.ToLower(); //兼容旧版本升级程序 2012/04/24
url = url.Replace("eas.smartupdateservice", "eas.updateservice");
Instance.service = CreateUpdateService(url);
}
static IUpdateService CreateUpdateService(string url)
{
System.ServiceModel.Channels.Binding binding = null;
string url2 = url.ToLower();
if (url2.StartsWith("net.tcp://"))
{
NetTcpBinding netTcpBinding = new NetTcpBinding();
binding = netTcpBinding;
netTcpBinding.MaxReceivedMessageSize = 64 * 1024 * 1024;
netTcpBinding.ReaderQuotas.MaxStringContentLength = 64 * 1024 * 1024;
netTcpBinding.ReaderQuotas.MaxArrayLength = 64 * 1024 * 1024;
netTcpBinding.Security.Mode = SecurityMode.None;
}
else if (url2.StartsWith("http://"))
{
if (url2.EndsWith(".asmx")) //.net2.0ws
{
return new Service_Proxy(url2);
}
else
{
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
binding = basicHttpBinding;
basicHttpBinding.MaxReceivedMessageSize = 64 * 1024 * 1024;
basicHttpBinding.ReaderQuotas.MaxStringContentLength = 64 * 1024 * 1024;
basicHttpBinding.ReaderQuotas.MaxArrayLength = 64 * 1024 * 1024;
basicHttpBinding.Security.Mode = BasicHttpSecurityMode.None;
}
}
else if (url2.StartsWith("net.msmq://"))
{
NetMsmqBinding netMsmqBinding = new NetMsmqBinding();
binding = netMsmqBinding;
netMsmqBinding.MaxReceivedMessageSize = 64 * 1024 * 1024;
netMsmqBinding.ReaderQuotas.MaxStringContentLength = 64 * 1024 * 1024;
netMsmqBinding.ReaderQuotas.MaxArrayLength = 64 * 1024 * 1024;
netMsmqBinding.Security.Mode = NetMsmqSecurityMode.None;
}
else if (url2.StartsWith("net.p2p://"))
{
NetMsmqBinding netPeerTcpBinding = new NetMsmqBinding();
binding = netPeerTcpBinding;
netPeerTcpBinding.MaxReceivedMessageSize = 64 * 1024 * 1024;
netPeerTcpBinding.ReaderQuotas.MaxStringContentLength = 64 * 1024 * 1024;
netPeerTcpBinding.ReaderQuotas.MaxArrayLength = 64 * 1024 * 1024;
netPeerTcpBinding.Security.Mode = NetMsmqSecurityMode.None;
}
else
throw new System.Exception("不是一个有效的升级服务地址");
ChannelFactory<IUpdateService> cf = new ChannelFactory<IUpdateService>(binding, url2);
return cf.CreateChannel();
}
internal static SmartConfig LoadSmartAssemblyList()
{
return SmartConfig.Load(Path.Combine(XUpdate.Instance.GetBaseDirectory(), SmartConfig.DefaultConfgiFile));
}
}
}