Display process of updating via console msg and hide control buttons for updater window.

main
muhammad.faique 2025-02-20 10:42:18 +05:00
parent 2f3e8e1aea
commit 21efe55c26
10 changed files with 135 additions and 57 deletions

Binary file not shown.

Binary file not shown.

View File

@ -3,27 +3,48 @@ 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];
//string destinationPath = args[1];
if (hwnd != IntPtr.Zero)
{
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 sourcePath = @"\\FileServer\\eData\\AVS";
string destinationPath = "C:\\Users\\Public\\Documents\\AVS"; string destinationPath = "C:\\Users\\Public\\Documents\\AVS";
// Define the application name
string applicationName = "AVS.exe"; string applicationName = "AVS.exe";
try try
@ -33,12 +54,15 @@ namespace AVSUpdater
{ {
Directory.CreateDirectory(destinationPath); Directory.CreateDirectory(destinationPath);
} }
MessageBox.Show("Start Copying Files"); Console.WriteLine("Updating AVS please wait!");
await Task.Delay(1000); // Delay to allow the updater to initialize
Console.WriteLine("Copying files.....................!");
// Copy all files from the source to the destination // Copy all files from the source to the destination
foreach (var file in Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories)) foreach (var file in Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories))
{ {
Console.WriteLine(".");
// Manually calculate the relative path // Manually calculate the relative path
string relativePath = file.Substring(sourcePath.Length + 1); // Remove the sourcePath part string relativePath = file.Substring(sourcePath.Length + 1); // Remove the sourcePath part
@ -58,18 +82,22 @@ namespace AVSUpdater
continue; continue;
} }
// Copy the file // Check if the file is open by another process
File.Copy(file, destFilePath, overwrite: true); 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 // Restart the main application
string applicationPath = Path.Combine(destinationPath, applicationName); string applicationPath = Path.Combine(destinationPath, applicationName);
Console.WriteLine(applicationPath);
if (System.IO.File.Exists(applicationPath))
if (File.Exists(applicationPath))
{ {
MessageBox.Show("Starting application again");
Process.Start(applicationPath); Process.Start(applicationPath);
} }
else else
@ -79,9 +107,59 @@ namespace AVSUpdater
} }
catch (Exception ex) catch (Exception ex)
{ {
// Show an error message if an exception occurs
Console.WriteLine($"An error occurred during the update: {ex.Message}"); Console.WriteLine($"An error occurred during the update: {ex.Message}");
} }
//Console.ReadKey(); await Task.Delay(1500);
Application.Exit();
}
}
// Method to check if a file is open by another process
public static bool IsFileOpen(string filePath)
{
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
}
}
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.