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
{
private static string m_DownLoadURL;
public static string DownLoadURL
{
get
{
if(m_DownLoadURL==null)
m_DownLoadURL= HttpUtils.GetDownloadUrl(Const.Key);
return m_DownLoadURL;
}
}
private static string m_ExecutablePath;
///
/// 获取目标文件路径 格式:D:\xx系统\wfy.exe
///
public static string ExecutablePath
{
get { return m_ExecutablePath; }
}
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;
}
}
///
/// 初始化目标文件配置
///
/// 目标文件路径
public static void Init(string executablePath)
{
m_ExecutablePath = executablePath;
m_ExecutableDirectory = null;
m_ExecutableName = null;
m_CurrentVersion = null;
}
///
/// 刷新目标程序版本
///
public static void RefreshVersion()
{
m_CurrentVersion = null;
}
}
}