123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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
- {
- /// <summary>
- /// 目标配置
- /// </summary>
- 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;
- /// <summary>
- /// 获取目标文件路径 格式:D:\xx系统\wfy.exe
- /// </summary>
- public static string ExecutablePath
- {
- get { return m_ExecutablePath; }
- }
-
- private static string m_ExecutableDirectory;
- /// <summary>
- /// 获取目标文件的父文件夹 格式:D:\xx系统\
- /// </summary>
- public static string ExecutableDirectory
- {
- get
- {
- return m_ExecutableDirectory ?? (m_ExecutableDirectory = FilePathUtil.GetDirectoryName(ExecutablePath));
- }
- }
- private static string m_ExecutableName;
- /// <summary>
- /// 获取目标文件的短文件名 格式:wfy.exe
- /// </summary>
- public static string ExecutableName
- {
- get { return m_ExecutableName ?? (m_ExecutableName = Path.GetFileName(ExecutablePath)); }
- }
- private static Version m_CurrentVersion;
- /// <summary>
- /// 获取目标进程文件的文件版本
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 初始化目标文件配置
- /// </summary>
- /// <param name="executablePath">目标文件路径</param>
- public static void Init(string executablePath)
- {
- m_ExecutablePath = executablePath;
- m_ExecutableDirectory = null;
- m_ExecutableName = null;
- m_CurrentVersion = null;
- }
- /// <summary>
- /// 刷新目标程序版本
- /// </summary>
- public static void RefreshVersion()
- {
- m_CurrentVersion = null;
- }
- }
- }
|