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.
192
Program.cs
192
Program.cs
|
@ -3,85 +3,163 @@ using System.IO;
|
|||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using static System.Net.WebRequestMethods;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace AVSUpdater
|
||||
{
|
||||
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)
|
||||
//{
|
||||
// Console.WriteLine("Invalid arguments. Usage: AVSUpdater.exe <sourcePath> <destinationPath>");
|
||||
// return;
|
||||
//}
|
||||
// Get handle to the console window
|
||||
IntPtr hwnd = GetConsoleWindow();
|
||||
|
||||
//string sourcePath = args[0];
|
||||
//string destinationPath = args[1];
|
||||
|
||||
|
||||
string sourcePath = @"\\FileServer\\eData\\AVS";
|
||||
string destinationPath = "C:\\Users\\Public\\Documents\\AVS";
|
||||
|
||||
|
||||
string applicationName = "AVS.exe";
|
||||
|
||||
try
|
||||
if (hwnd != IntPtr.Zero)
|
||||
{
|
||||
// Ensure the destination directory exists
|
||||
if (!Directory.Exists(destinationPath))
|
||||
int style = GetWindowLong(hwnd, GWL_STYLE);
|
||||
|
||||
// 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
|
||||
string destDirectory = Path.GetDirectoryName(destFilePath);
|
||||
if (!Directory.Exists(destDirectory))
|
||||
if (!Directory.Exists(destinationPath))
|
||||
{
|
||||
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
|
||||
File.Copy(file, destFilePath, overwrite: true);
|
||||
if (System.IO.File.Exists(applicationPath))
|
||||
{
|
||||
Process.Start(applicationPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Application executable not found in the destination folder.");
|
||||
}
|
||||
}
|
||||
|
||||
// Restart the main application
|
||||
string applicationPath = Path.Combine(destinationPath, applicationName);
|
||||
Console.WriteLine(applicationPath);
|
||||
|
||||
|
||||
if (File.Exists(applicationPath))
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("Starting application again");
|
||||
Process.Start(applicationPath);
|
||||
// Show an error message if an exception occurs
|
||||
Console.WriteLine($"An error occurred during the update: {ex.Message}");
|
||||
}
|
||||
else
|
||||
await Task.Delay(1500);
|
||||
Application.Exit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Method to check if a file is open by another process
|
||||
public static bool IsFileOpen(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Application executable not found in the destination folder.");
|
||||
|
||||
|
||||
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Write, FileShare.None))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"An error occurred during the update: {ex.Message}");
|
||||
|
||||
|
||||
// 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