Display process of updating via console msg and hide control buttons for updater window.
parent
2f3e8e1aea
commit
21efe55c26
Binary file not shown.
Binary file not shown.
Binary file not shown.
194
Program.cs
194
Program.cs
|
@ -3,85 +3,163 @@ using System.IO;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using static System.Net.WebRequestMethods;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace AVSUpdater
|
namespace AVSUpdater
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
|
||||||
|
// Windows API functions for manipulating window styles
|
||||||
|
// P/Invoke to get the handle to the console window from kernel32.dll
|
||||||
|
[DllImport("kernel32.dll", SetLastError = true)]
|
||||||
|
static extern IntPtr GetConsoleWindow();
|
||||||
|
|
||||||
|
// Windows API functions for manipulating window styles (user32.dll)
|
||||||
|
[DllImport("user32.dll", SetLastError = true)]
|
||||||
|
static extern int GetWindowLong(IntPtr hwnd, int nIndex);
|
||||||
|
|
||||||
|
[DllImport("user32.dll", SetLastError = true)]
|
||||||
|
static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
|
||||||
|
|
||||||
|
// Constants for window styles
|
||||||
|
const int GWL_STYLE = -16;
|
||||||
|
const int WS_SYSMENU = 0x00080000;
|
||||||
|
|
||||||
|
|
||||||
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
//if (args.Length < 2)
|
// Get handle to the console window
|
||||||
//{
|
IntPtr hwnd = GetConsoleWindow();
|
||||||
// Console.WriteLine("Invalid arguments. Usage: AVSUpdater.exe <sourcePath> <destinationPath>");
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
|
|
||||||
//string sourcePath = args[0];
|
if (hwnd != IntPtr.Zero)
|
||||||
//string destinationPath = args[1];
|
|
||||||
|
|
||||||
|
|
||||||
string sourcePath = @"\\FileServer\\eData\\AVS";
|
|
||||||
string destinationPath = "C:\\Users\\Public\\Documents\\AVS";
|
|
||||||
|
|
||||||
|
|
||||||
string applicationName = "AVS.exe";
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
// Ensure the destination directory exists
|
int style = GetWindowLong(hwnd, GWL_STYLE);
|
||||||
if (!Directory.Exists(destinationPath))
|
|
||||||
|
// Remove the system menu style (which includes the close button)
|
||||||
|
SetWindowLong(hwnd, GWL_STYLE, style & ~WS_SYSMENU);
|
||||||
|
// Define the source and destination paths
|
||||||
|
string sourcePath = @"\\FileServer\\eData\\AVS";
|
||||||
|
string destinationPath = "C:\\Users\\Public\\Documents\\AVS";
|
||||||
|
|
||||||
|
// Define the application name
|
||||||
|
string applicationName = "AVS.exe";
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(destinationPath);
|
|
||||||
}
|
|
||||||
MessageBox.Show("Start Copying Files");
|
|
||||||
// Copy all files from the source to the destination
|
|
||||||
foreach (var file in Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories))
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
// Manually calculate the relative path
|
|
||||||
string relativePath = file.Substring(sourcePath.Length + 1); // Remove the sourcePath part
|
|
||||||
|
|
||||||
// Construct the destination file path
|
|
||||||
string destFilePath = Path.Combine(destinationPath, relativePath);
|
|
||||||
|
|
||||||
// Ensure the destination directory exists
|
// Ensure the destination directory exists
|
||||||
string destDirectory = Path.GetDirectoryName(destFilePath);
|
if (!Directory.Exists(destinationPath))
|
||||||
if (!Directory.Exists(destDirectory))
|
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(destDirectory);
|
Directory.CreateDirectory(destinationPath);
|
||||||
}
|
}
|
||||||
|
Console.WriteLine("Updating AVS please wait!");
|
||||||
|
|
||||||
// Skip specific files (e.g., alert-alarm.WAV)
|
|
||||||
if (relativePath.Equals("alert-alarm.WAV", StringComparison.OrdinalIgnoreCase))
|
await Task.Delay(1000); // Delay to allow the updater to initialize
|
||||||
|
Console.WriteLine("Copying files.....................!");
|
||||||
|
// Copy all files from the source to the destination
|
||||||
|
foreach (var file in Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories))
|
||||||
{
|
{
|
||||||
continue;
|
Console.WriteLine(".");
|
||||||
|
// Manually calculate the relative path
|
||||||
|
string relativePath = file.Substring(sourcePath.Length + 1); // Remove the sourcePath part
|
||||||
|
|
||||||
|
// Construct the destination file path
|
||||||
|
string destFilePath = Path.Combine(destinationPath, relativePath);
|
||||||
|
|
||||||
|
// Ensure the destination directory exists
|
||||||
|
string destDirectory = Path.GetDirectoryName(destFilePath);
|
||||||
|
if (!Directory.Exists(destDirectory))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(destDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip specific files (e.g., alert-alarm.WAV)
|
||||||
|
if (relativePath.Equals("alert-alarm.WAV", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the file is open by another process
|
||||||
|
if (IsFileOpen(file))
|
||||||
|
{
|
||||||
|
// Close the file
|
||||||
|
CloseFile(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the file
|
||||||
|
System.IO.File.Copy(file, destFilePath, overwrite: true);
|
||||||
}
|
}
|
||||||
|
Console.WriteLine("Copy completed!");
|
||||||
|
// Restart the main application
|
||||||
|
string applicationPath = Path.Combine(destinationPath, applicationName);
|
||||||
|
|
||||||
// Copy the file
|
if (System.IO.File.Exists(applicationPath))
|
||||||
File.Copy(file, destFilePath, overwrite: true);
|
{
|
||||||
|
Process.Start(applicationPath);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Application executable not found in the destination folder.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
// Restart the main application
|
|
||||||
string applicationPath = Path.Combine(destinationPath, applicationName);
|
|
||||||
Console.WriteLine(applicationPath);
|
|
||||||
|
|
||||||
|
|
||||||
if (File.Exists(applicationPath))
|
|
||||||
{
|
{
|
||||||
MessageBox.Show("Starting application again");
|
// Show an error message if an exception occurs
|
||||||
Process.Start(applicationPath);
|
Console.WriteLine($"An error occurred during the update: {ex.Message}");
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("Application executable not found in the destination folder.");
|
|
||||||
}
|
}
|
||||||
|
await Task.Delay(1500);
|
||||||
|
Application.Exit();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Method to check if a file is open by another process
|
||||||
|
public static bool IsFileOpen(string filePath)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"An error occurred during the update: {ex.Message}");
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Write, FileShare.None))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Method to close a file that is open by another process
|
||||||
|
private static void CloseFile(string filePath)
|
||||||
|
{
|
||||||
|
var processes = Process.GetProcesses();
|
||||||
|
foreach (var process in processes)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var files = process.Modules; // Define 'files' here
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
var processModule = file as ProcessModule;
|
||||||
|
if (processModule != null && processModule.FileName.ToLower() == filePath.ToLower())
|
||||||
|
{
|
||||||
|
process.Kill();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// Ignore exceptions
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//Console.ReadKey();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue