using System; using System.Collections.Generic; using System.Linq; using System.Text; using Yaulw.Process; using Yaulw.Tools; using System.IO; using Yaulw.File; using Yaulw.Xml; using System.Reflection; namespace Installables.All { /// /// Common Functions and Helpers Useful for all Installing activities. /// public static class Common { #region Public Definitions public const string INSTALLED_COMPONENT_CONFIG_XML_FILENAME = "InstalledComponentConfig.xml"; public const string EMBEDDED_COMPONENT_CONFIG_XML_FILENAME = "EmbeddedComponentConfig.xml"; /// /// Allow Setting of Log by external Assembly /// public static Logging Log { get { return s_Log; } set { if (value != null) s_Log = value; } } #endregion #region Private Statics private static ISReadWrite s_isrw = null; private static XSerializer s_serializer = null; private static ComponentConfig s_EmbeddedComponentConfig = null; private static ComponentConfig s_InstalledComponentConfig = null; private static Logging s_Log = null; #endregion #region Construction /// /// Responsible for reading in embedded and installed configuration /// static Common() { s_isrw = new ISReadWrite(INSTALLED_COMPONENT_CONFIG_XML_FILENAME); s_serializer = new XSerializer(); //# Read in from Resource (Embedded Components) s_EmbeddedComponentConfig = s_serializer.ReadFromResource(Assembly.GetExecutingAssembly().GetManifestResourceStream("Installables.All." + EMBEDDED_COMPONENT_CONFIG_XML_FILENAME)); if (s_EmbeddedComponentConfig == null) throw new Exception("Could not read in EmbeddedComponentConfig"); // should never happen //# Read in from IS (Currently Installed Components) s_InstalledComponentConfig = s_isrw.ReadFromIS(); if (s_InstalledComponentConfig == null) s_InstalledComponentConfig = new ComponentConfig(); } #endregion #region Public Statics /// /// Retrieve the EmbeddedComponentConfig /// /// the EmbeddedComponentConfig public static ComponentConfig EmbeddedConfig { get { return s_EmbeddedComponentConfig; } } /// /// Retrieve the InstalledComponentConfig /// /// the InstalledComponentConfig or null, if not existent public static ComponentConfig InstalledConfig { get { return s_InstalledComponentConfig; } } /// /// Allows Caller to write out any changes to InstalledConfig back to the File /// public static void WriteOutChangesToInstalledConfig() { s_isrw.WriteToIS(s_InstalledComponentConfig); } /// /// Checks to see if any Components are installed. If this returns false, then this is a Fresh Install /// /// true, if any components are installed, false otherwise public static bool AreAnyComponentsInstalled() { bool bIsInstalled = (InstalledConfig != null) && (InstalledConfig.BinaryComponents.Components.Length > 0 || InstalledConfig.SetupComponents.Components.Length > 0); return bIsInstalled; } /// /// Retrieves the Index for the Component that matches the specified Unique Label /// /// label to search components for /// a component array /// a value >=0 or -1, if not found public static int GetIndexForComponentUniqueLabel(string UniqueLabel, ComponentConfig.Component[] components) { if (String.IsNullOrEmpty(UniqueLabel) || components == null || components.Length <= 0) return -1; for (int i = 0; i < components.Length; ++i) { if (String.Compare(components[i].UniqueLabel, UniqueLabel, true) == 0) return i; } return -1; } /// /// Spawn a Setup Process (Setup.exe for example) /// public static void PSetupSpwan(string SetupFileNameNPath, string param_s) { PStarter.StartProcess(PStartInfo.CreateProcess(SetupFileNameNPath, param_s, "", true, System.Diagnostics.ProcessWindowStyle.Hidden, false), true, false); } /// /// Spawn a MSI Setup Process (*.msi) /// public static void PSetupMSIEXEC(string param_s) { string msiexec = System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\msiexec.exe"; PStarter.StartProcess(PStartInfo.CreateProcess(msiexec, param_s, "", true, System.Diagnostics.ProcessWindowStyle.Hidden, false), true, false); } /// /// Run a command on the commandline * Hidden * /// /// cmd to run public static string RunCmdLine(string cmdline) { string result = PStarter.RunDosCommand(cmdline); return result; } /// /// Use this to get the complete path to a .net framework utility like gacutil.exe or /// installutil.exe.. any .net framework utility. Will fall back to look in the %temp% folder, /// if not found, giving the opportunity to deploy the util directly with the components /// /// the utility in .net you are looking for like gacutil.exe /// the full filenameNpath or "", if no existing file found public static string GetNetFrameworkUtilFileNameNPathFile(string UtilFileName) { string windir = System.Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine); string NetFramework1_0 = windir + "\\Microsoft.Net\\Framework\\v1.0.3705"; string NetFramework1_1 = windir + "\\Microsoft.Net\\Framework\\v1.1.4322"; string NetFramework2_0 = windir + "\\Microsoft.Net\\Framework\\v2.0.50727"; string NetFramework3_0 = windir + "\\Microsoft.Net\\Framework\\v3.0"; string NetFramework3_5 = windir + "\\Microsoft.Net\\Framework\\v3.5"; string NetFramework4_0 = windir + "\\Microsoft.Net\\Framework\\v4.0.30319"; string TempPath = PathNaming.PathEndsWithNoSlash(Path.GetTempPath()); // We use this as a Fallback, in case file doesn't exist in the framework string[] Frameworks = new string[] { NetFramework2_0, NetFramework4_0, NetFramework1_0, NetFramework1_1, NetFramework3_0, NetFramework3_5, TempPath }; string NetUtilFileNameNPath = ""; foreach (string framework in Frameworks) { if (File.Exists(framework + "\\" + UtilFileName)) { NetUtilFileNameNPath = framework + "\\" + UtilFileName; return NetUtilFileNameNPath; } }; return NetUtilFileNameNPath; } /// /// Quick Helper to get the Program Files Path for the specific system /// /// Program Files path with a '/' at the end public static string GetProgramFilesPathOnSystemWithEndSlash() { string ProgramFiles = System.Environment.GetEnvironmentVariable("ProgramFiles(x86)"); if (String.IsNullOrEmpty(ProgramFiles)) ProgramFiles = System.Environment.GetEnvironmentVariable("ProgramFiles"); return PathNaming.PathEndsWithSlash(ProgramFiles); } /// /// /// /// public static string SetOwnership() { // in order to do any of this, sadly, we must first install the windows resource kit //subinacl /subdirectories *.* /setowner=domainname\user return String.Empty; } /// /// To grant the specified User or group Full Control permissions to the folder and its contents /// /// full path to folder/directory /// domainname\administrator, any windows user or group /// public static bool GrantFullPermissionToFolderForUserOrGroup(string FolderNPath, string UserOrGroup) { if (Directory.Exists(FolderNPath)) { string command = String.Format("cacls \"{0}\" /t /e /g {1}:f", FolderNPath, UserOrGroup); if (command.Contains("Invalid arguments.")) return false; else return true; } return false; } /// /// Stop a service /// /// name of service to stop /// true if successful, false otherwise public static bool StopService(string ServiceName) { bool bSuccess = true; if (ServiceW.DoesServiceExist(ServiceName)) bSuccess = ServiceW.StopService(ServiceName, true, 120); return bSuccess; } /// /// start a service /// /// name of a service to start /// true if successful, false otherwise public static bool StartService(string ServiceName) { bool bSuccess = true; if (ServiceW.DoesServiceExist(ServiceName)) bSuccess = ServiceW.StartService(ServiceName, 120); return bSuccess; } /// /// Does Service Exist /// /// Name of service to check /// true if successful, false otherwise public static bool ServiceExists(string ServiceName) { return ServiceW.DoesServiceExist(ServiceName); } #endregion } }