3 using System.Collections.Generic;
8 using System.Text.RegularExpressions;
11 namespace Sage.ES.S50.Launch
16 private const string _cNameProd =
"Sage 50";
17 private const int nPrimeraVersionSOCompatible = 10240;
23 static void Main(String[] args)
30 string lcProcessName, lcVersion, lcLibrary, lcClass, lcPath, lcArguments =
"";
32 lcVersion = DirectoryNameVersion(Directory.GetCurrentDirectory());
33 lcPath = Path.Combine(Directory.GetCurrentDirectory(), lcVersion);
34 lcLibrary = Path.Combine(lcPath,
"Sage.50.exe");
35 lcClass =
"Sage.ES.S50.S50Update.Classes.S50UpdateLibrary";
36 if (HasArgument(args,
"NOVALIDATE") || ValidateSOFramework(lcVersion))
38 if (HasArgument(args,
"WAIT5"))
41 System.Threading.Thread.Sleep(5000);
43 args = args.Where(x => x !=
"WAIT5").ToArray();
46 if (File.Exists(lcLibrary))
49 Directory.SetCurrentDirectory(lcPath);
51 Process loStart =
new System.Diagnostics.Process();
52 ProcessStartInfo loStartInfo;
54 lcProcessName =
"SageUpdate.exe";
55 lcArguments =
string.Format(
" \"{0}\" \"{1}\" ", lcClass, lcLibrary);
56 lcArguments +=
string.Join(
" ", args);
60 loStartInfo =
new System.Diagnostics.ProcessStartInfo(lcProcessName);
61 loStartInfo.Arguments = lcArguments;
62 loStartInfo.WorkingDirectory = lcPath;
63 loStart.StartInfo = loStartInfo;
72 throw new Exception(String.Format(
"File not found {0}", lcLibrary));
78 _SetEventLog(ex.Message);
84 private static bool HasArgument(
string[] toArgs,
string tcKey)
86 return toArgs.Length > 0 && toArgs.Any(f => f.ToUpper() == tcKey);
89 private static bool ValidateSOFramework(
string tcVersion)
93 Version loVersion, loCuttingVersion;
97 if (!
string.IsNullOrEmpty(tcVersion) && tcVersion !=
"librerias")
99 loVersion =
new Version(tcVersion);
100 loCuttingVersion =
new Version(50, 7810, 0);
102 if (loVersion >= loCuttingVersion)
104 if ((lnVersion = _GetValueRegistry(
@"SOFTWARE\Microsoft\Windows NT\CurrentVersion",
"CurrentBuildNumber")) < nPrimeraVersionSOCompatible)
106 lcError = $
"La versión de Windows de este ordenador ({lnVersion}) no es compatible con {_cNameProd}.";
107 lcError = $
"{lcError}{Environment.NewLine}{Environment.NewLine}Solo es posible acceder a {_cNameProd} desde ordenadores con una versión de Windows igual o superior a Windows 10 o Windows Server 2016.";
109 else if (_GetValueRegistry(
"SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\",
"Release") < 528040)
111 lcError = $
"El ordenador no tiene instalado Framework.NET 4.8.";
112 lcError = $
"{lcError}{Environment.NewLine}{Environment.NewLine}Solo es posible acceder a {_cNameProd} si el ordenador tiene instalado el Framework.NET 4.8";
115 if (!
string.IsNullOrEmpty(lcError))
117 lcError = $
"No es posible acceder a Sage 50.{Environment.NewLine}{Environment.NewLine}{lcError}";
118 MessageBox.Show(lcError,
"Atención", MessageBoxButtons.OK, MessageBoxIcon.Error);
119 _SetEventLog(lcError);
133 private static void _UpdateSageUpdate(String tcLibrary, String tcClass)
137 Assembly assembly = Assembly.LoadFrom(tcLibrary);
138 Type elementType = assembly.GetType(tcClass);
140 if (elementType is Type)
143 if (elementType.GetMethod(
"UpdateSageUpdate") is MethodInfo)
145 dynamic updateLibrary = Activator.CreateInstance(elementType,
new object[] { Directory.GetCurrentDirectory() });
146 updateLibrary.UpdateSageUpdate();
152 _SetEventLog(ex.Message);
157 private static String DirectoryNameVersion(String path)
159 List<DirectoryInfo> loListDirs;
160 String pathDirectoryVersion =
"";
161 String[] listDirectories = Directory.GetDirectories(path,
"50.*");
163 if (listDirectories.Length > 0)
165 loListDirs = (from lcDirName in listDirectories
166 select
new DirectoryInfo(lcDirName)).ToList();
169 pathDirectoryVersion = (from loDir in loListDirs
170 where Regex.IsMatch(loDir.Name,
@"^50\.[0-9]{4}\.[0-9]{1,3}$")
171 && File.Exists(Path.Combine(loDir.FullName,
"sage.50.exe"))
172 orderby loDir.Name descending
173 select loDir.Name).FirstOrDefault();
176 if (String.IsNullOrEmpty(pathDirectoryVersion)) pathDirectoryVersion =
"librerias";
178 return pathDirectoryVersion;
181 private static void _SetEventLog(String tcMessage)
183 String source =
"Sage 50c";
184 if (!EventLog.SourceExists(source)) EventLog.CreateEventSource(source,
"Application");
185 EventLog.WriteEntry(source, tcMessage,
System.Diagnostics.EventLogEntryType.Error);
188 private static int _GetValueRegistry(
string tcKey,
string tcRegistry)
190 using (var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(tcKey))
192 return int.TryParse(Convert.ToString(key?.GetValue(tcRegistry)), out
int lnVersion) ? lnVersion : 0;