服务端配置管理
配置文件
<appSettings>
<add key="VersionDir" value="E:\Test\XXXX网络版软件版本库" />
<add key="MainVersionFile" value="XXXX.XXXX.exe"/>
</appSettings>
配置文件读取
public static string GetConfigAbsolutePath(string sKey)
{
var sPath = GetConfigString(sKey);
if (!string.IsNullOrEmpty(sPath))
{
if (sPath.StartsWith("~"))
{
sPath = System.Web.Hosting.HostingEnvironment.MapPath(sPath);
}
}
else
{
sPath = "";
}
return sPath;
}
public static string GetConfigString(string sKey)
{
return System.Configuration.ConfigurationManager.AppSettings[sKey]?.ToString().Trim();
}
服务接口开发
public class UpgradeController : ApiController
{
[HttpGet]
[ResponseType(typeof(ResponseInfo))]
public IHttpActionResult Info()
{
try
{
return Json(new ResponseOk() { Data = UpgradeHelper.GetUpgradeInfo() } as ResponseInfo);
}
catch (Exception ex)
{
ExceptionHelper.LogActionException(ex);
return Json(new ResponseError(ex) as ResponseInfo);
}
}
[HttpGet]
[ResponseType(typeof(ResponseInfo))]
public IHttpActionResult Abstract()
{
try
{
return Json(new ResponseOk() { Data = new UpgradeInfoAbstract(UpgradeHelper.GetUpgradeInfo()) } as ResponseInfo);
}
catch (Exception ex)
{
ExceptionHelper.LogActionException(ex);
return Json(new ResponseError(ex) as ResponseInfo);
}
}
[HttpGet]
[ResponseType(typeof(ResponseInfo))]
public IHttpActionResult Files()
{
try
{
return Json(new ResponseOk() { Data = new UpgradeFilesInfo(UpgradeHelper.GetUpgradeInfo()) } as ResponseInfo);
}
catch (Exception ex)
{
ExceptionHelper.LogActionException(ex);
return Json(new ResponseError(ex) as ResponseInfo);
}
}
[HttpGet]
[ResponseType(typeof(ResponseMessageResult))]
public IHttpActionResult Stream([FromUri]string param)
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
try
{
if (string.IsNullOrEmpty(param))
{
throw new Exception("参数为空");
}
var sPath = Path.Combine(UpgradeHelper.LatestVersionFolder, param.ToString());
if (!File.Exists(sPath))
{
throw new Exception($"未能找到文件{sPath}");
}
FileStream fileStream = new FileStream(sPath, FileMode.Open);
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = param
};
}
catch (Exception ex)
{
ExceptionHelper.LogActionException(ex);
response.StatusCode = HttpStatusCode.NotFound;
response.Content = null;
}
return ResponseMessage(response);
}
}
辅助方法类
public class UpgradeHelper
{
private static string _versionFolder = "";
public static string VersionFolder
{
get
{
_versionFolder = Path.Combine(GlobalHelper.GetConfigAbsolutePath("VersionDir"));
if (!Directory.Exists(_versionFolder))
Directory.CreateDirectory(_versionFolder);
return _versionFolder;
}
}
private static string _latestVersionFolder = "";
public static string LatestVersionFolder
{
get
{
_latestVersionFolder = GetLatestVersionFolder(VersionFolder);
if (!Directory.Exists(_latestVersionFolder))
Directory.CreateDirectory(_latestVersionFolder);
return _latestVersionFolder;
}
}
public static string GetLatestVersionFolder(string sVersionFolder)
{
var sLatestVersionFolder = "";
try
{
if (string.IsNullOrEmpty(sVersionFolder))
{
throw new Exception("根目录为空");
}
if (!Directory.Exists(sVersionFolder))
{
throw new Exception($"目录不存在{sVersionFolder}");
}
var dirInfo = new DirectoryInfo(sVersionFolder);
var lstDir = dirInfo.GetDirectories();
if (null == lstDir || 0 >= lstDir.Count())
{
throw new Exception($"目录{sVersionFolder}不存在下级目录");
}
SortByLastWriteTimeDesc(ref lstDir);
sLatestVersionFolder = lstDir.ElementAt(0).FullName;
}
catch (Exception ex)
{
Log.Loging.Error(ex.Message);
}
return sLatestVersionFolder;
}
private static void SortByLastWriteTimeDesc(ref DirectoryInfo[] dirs)
{
Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return y.LastWriteTime.CompareTo(x.LastWriteTime); });
}
public static List<string> GetOffspringFiles(string sDir)
{
var lstFile = new List<string>();
try
{
var dirInfo = new DirectoryInfo(sDir);
var lstFileInfo = dirInfo.EnumerateFiles("*", SearchOption.AllDirectories)?.ToList();
if (null != lstFileInfo && 0 < lstFileInfo.Count)
{
lstFileInfo.ForEach(p =>
{
lstFile.Add(p.FullName.Substring(sDir.Length).TrimStart('\\'));
});
}
}
catch (Exception ex)
{
ExceptionHelper.LogActionException(ex);
lstFile.Clear();
}
return lstFile;
}
public static UpgradeInfo GetUpgradeInfo(string sVersionFolder, string sMainVersionFile)
{
var upgradeInfo = new UpgradeInfo();
var lstFile = GetOffspringFiles(sVersionFolder);
foreach (string sRelativePath in lstFile)
{
upgradeInfo.FileUnits.Add(new FileUnit(sVersionFolder, Path.Combine(sVersionFolder, sRelativePath.TrimStart('\\'))));
}
((List<FileUnit>)(upgradeInfo.FileUnits)).Sort();
string sMainFullPath = null;
if (!string.IsNullOrEmpty(sMainVersionFile))
{
var sRelativePath = lstFile.FirstOrDefault(p => p.Contains(sMainVersionFile))?.TrimStart('\\');
if (!string.IsNullOrEmpty(sRelativePath))
{
sMainFullPath = Path.Combine(sVersionFolder, sRelativePath);
if (!File.Exists(sMainFullPath))
{
sMainFullPath = null;
}
}
}
var sLatestMainVersion = "N/A";
if (!string.IsNullOrEmpty(sMainFullPath))
{
var fileInfo = new FileInfo(sMainFullPath);
sLatestMainVersion = $"{fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length)} {FileUnit.GetFileVersion(sMainFullPath)}";
upgradeInfo.MainVersionInfo = FileUnit.GetFileVersionInfo(sMainFullPath);
}
return upgradeInfo;
}
public static UpgradeInfo GetUpgradeInfo()
{
return GetUpgradeInfo(LatestVersionFolder, GlobalHelper.GetConfigString("MainVersionFile"));
}
}
接口页面
