InventoryAgent/Inventory.Core/UpdateChecker.cs
2025-10-20 00:03:49 +08:00

59 lines
1.8 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text.Json;
namespace Inventory.Core
{
public class VersionManifest
{
public string Version { get; set; }
public string MsiPath { get; set; }
}
public static class UpdateChecker
{
public static bool CheckForUpdate(string manifestPath, out string newMsiPath)
{
newMsiPath = null;
try
{
// 1. Get local assembly version
var localVersion = Assembly.GetExecutingAssembly().GetName().Version;
if (!File.Exists(manifestPath))
{
// Manifest not found, cannot check for update.
return false;
}
// 2. Read and deserialize the version.json
var manifestContent = File.ReadAllText(manifestPath);
var manifest = JsonSerializer.Deserialize<VersionManifest>(manifestContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (manifest == null || !Version.TryParse(manifest.Version, out var manifestVersion))
{
// Invalid manifest file
return false;
}
// 3. Compare versions
if (manifestVersion > localVersion)
{
// 4. If newer, construct the full MSI path
var manifestDir = Path.GetDirectoryName(manifestPath);
newMsiPath = Path.Combine(manifestDir, manifest.MsiPath);
return true;
}
}
catch (Exception)
{
// Log this exception in a real application
return false;
}
return false;
}
}
}