using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Update.Util;
namespace Update.Config
{
///
/// 目标配置
///
public static class HostConfig
{
///
/// 默认目标文件名
///
public const string DEFAULT_NAME = "OutputDll/OutputDll/SAGA.MBI.exe";
private static string m_ExecutablePath;
///
/// 获取目标文件路径 格式:D:\xx系统\wfy.exe
///
public static string ExecutablePath
{
get
{
return m_ExecutablePath ?? (m_ExecutablePath = AppConfig.ExecutablePath);
}
}
private static string m_ExecutableConfigPath;
///
/// 获取目标配置文件路径 格式:D:\xx系统\wfy.exe.config
///
public static string ExecutableConfigPath
{
get
{
return m_ExecutableConfigPath ?? (m_ExecutableConfigPath = ExecutablePath + ".config");
}
}
private static string m_ExecutableDirectory;
///
/// 获取目标文件的父文件夹 格式:D:\xx系统\
///
public static string ExecutableDirectory
{
get
{
return m_ExecutableDirectory ?? (m_ExecutableDirectory = FilePathUtil.GetDirectoryName(ExecutablePath));
}
}
private static string m_ExecutableName;
///
/// 获取目标文件的短文件名 格式:wfy.exe
///
public static string ExecutableName
{
get { return m_ExecutableName ?? (m_ExecutableName = Path.GetFileName(ExecutablePath)); }
}
private static Version m_CurrentVersion;
///
/// 获取目标进程文件的文件版本
///
public static Version CurrentVersion
{
get
{
if (m_CurrentVersion == null)
{
//FileInfo fileInfo=new FileInfo(Assembly.GetExecutingAssembly().Location);
//var dirPath = fileInfo.DirectoryName;
//Program Files文件夹, Copy 需要管理员权限,更改为临时目录
var dirPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string copyDestPath = Path.Combine(dirPath, ExecutableName);
//由于加载的程序集无法卸载,所以将dll,Copy出来
try
{
File.Copy(ExecutablePath, copyDestPath, true);
}
catch (Exception e)
{
//MessageBox.Show("Copy Fail"+e.StackTrace);
}
if (File.Exists(copyDestPath))
m_CurrentVersion = Assembly.ReflectionOnlyLoadFrom(copyDestPath).GetName().Version;
}
return m_CurrentVersion;
}
}
private static string m_UpdateUrl;
///
/// 获取目标进程配置文件中的 UpdateUrl 配置项,获取后会自动处理末尾,保证返回时以 '/' 结尾(如果没配置将返回 null)
///
public static string UpdateUrl
{
get
{
if (m_UpdateUrl == null)
{
string url = ReadHostConfig("UpdateUrl");
if (string.IsNullOrWhiteSpace(url))
return null;
url = url.Trim();
m_UpdateUrl = url.EndsWith("/") ? url : (url + "/");
}
return m_UpdateUrl;
}
}
public static string BaseUrl = Const.URL;
///
/// 初始化目标文件配置
///
/// 目标文件路径
public static void Init(string executablePath)
{
m_ExecutablePath = executablePath;
m_ExecutableConfigPath = null;
m_ExecutableDirectory = null;
m_ExecutableName = null;
m_CurrentVersion = null;
m_UpdateUrl = null;
}
///
/// 刷新目标程序版本
///
public static void RefreshVersion()
{
m_CurrentVersion = null;
}
//读取配置
private static string ReadHostConfig(string key)
{
try
{
//Configuration config = ConfigurationManager.OpenExeConfiguration(ExecutablePath);
//KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
//KeyValueConfigurationElement add = appSettings.Cast().FirstOrDefault(element => string.Equals(element.Key, key, StringComparison.OrdinalIgnoreCase));
//return add == null ? null : add.Value;
return "http://localhost:8888/AutoUpdate/";
}
catch
{
return null;
}
}
}
}