diff --git a/.vs/AVSUpdater/FileContentIndex/36a5ff39-f236-480f-b40b-476e342c1cb0.vsidx b/.vs/AVSUpdater/FileContentIndex/36a5ff39-f236-480f-b40b-476e342c1cb0.vsidx deleted file mode 100644 index 79e769a..0000000 Binary files a/.vs/AVSUpdater/FileContentIndex/36a5ff39-f236-480f-b40b-476e342c1cb0.vsidx and /dev/null differ diff --git a/.vs/AVSUpdater/v16/.suo b/.vs/AVSUpdater/v16/.suo index 5d1556e..616c61e 100644 Binary files a/.vs/AVSUpdater/v16/.suo and b/.vs/AVSUpdater/v16/.suo differ diff --git a/.vs/AVSUpdater/v17/.suo b/.vs/AVSUpdater/v17/.suo index 565d5a6..0b94430 100644 Binary files a/.vs/AVSUpdater/v17/.suo and b/.vs/AVSUpdater/v17/.suo differ diff --git a/Program.cs b/Program.cs index 5c0507d..e39d4d6 100644 --- a/Program.cs +++ b/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 "); - // 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(); } } -} + diff --git a/bin/Debug/AVSUpdater.exe b/bin/Debug/AVSUpdater.exe index e13550c..af8b38a 100644 Binary files a/bin/Debug/AVSUpdater.exe and b/bin/Debug/AVSUpdater.exe differ diff --git a/bin/Debug/AVSUpdater.pdb b/bin/Debug/AVSUpdater.pdb index 24af097..a8d2557 100644 Binary files a/bin/Debug/AVSUpdater.pdb and b/bin/Debug/AVSUpdater.pdb differ diff --git a/obj/Debug/AVSUpdater.csproj.AssemblyReference.cache b/obj/Debug/AVSUpdater.csproj.AssemblyReference.cache index 68de2bd..fb06750 100644 Binary files a/obj/Debug/AVSUpdater.csproj.AssemblyReference.cache and b/obj/Debug/AVSUpdater.csproj.AssemblyReference.cache differ diff --git a/obj/Debug/AVSUpdater.exe b/obj/Debug/AVSUpdater.exe index e13550c..af8b38a 100644 Binary files a/obj/Debug/AVSUpdater.exe and b/obj/Debug/AVSUpdater.exe differ diff --git a/obj/Debug/AVSUpdater.pdb b/obj/Debug/AVSUpdater.pdb index 24af097..a8d2557 100644 Binary files a/obj/Debug/AVSUpdater.pdb and b/obj/Debug/AVSUpdater.pdb differ diff --git a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index f45cca5..0f973ac 100644 Binary files a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ