using System; 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 { // 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) { // Get handle to the console window IntPtr hwnd = GetConsoleWindow(); 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 destinationPath = "C:\\Users\\Public\\Documents\\AVS"; // Define the application name string applicationName = "AVS.exe"; try { // Ensure the destination directory exists if (!Directory.Exists(destinationPath)) { Directory.CreateDirectory(destinationPath); } 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 foreach (var file in Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories)) { 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); if (System.IO.File.Exists(applicationPath)) { Process.Start(applicationPath); } else { Console.WriteLine("Application executable not found in the destination folder."); } } catch (Exception ex) { // Show an error message if an exception occurs Console.WriteLine($"An error occurred during the update: {ex.Message}"); } 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 } } } } }