/// <summary>
		/// Executes a search and returns 
		/// </summary>
		/// <param name="search"></param>
		/// <returns></returns>
		public ArrayList SearchForTypes(Search search, Type type, IProgressViewer progressViewer)
		{			
			_array = new ArrayList();
			_progressViewer = progressViewer;

			try
			{
				if (search == null)
					throw new ArgumentNullException("search", "The search cannot be null.");

				if (type == null)
					throw new ArgumentNullException("type", "The type to search for cannot be null.");

				// save the type that we are searching for
				_type = type;

				// bind to the search's events
				search.FileFound += new SearchEventHandler(this.OnFileFound);

				// start the search
				search.FindFiles();				
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
			// return the array list of items found
			return _array;
		}
		/// <summary>
		/// Queries the latest version of a particular product
		/// </summary>
		/// <param name="updatesPath">The path to the updates</param>
		/// <param name="productName">The name of the product to update</param>
		/// <param name="currentVersion">The current version of the product that is checking for updates</param>
		/// <param name="productId">The id of the product that is checking for updates</param>
		/// <returns></returns>		
		public static XmlDocument QueryLatestVersionEx(string updatesPath, string productName, string currentVersion, string productId)
		{
			/*
			 * we really don't need all the information, but it would be nice to log who is trying to update
			 * */			
			try
			{				
				// log information about this event to the system's event log
//				Debug.WriteLine(string.Format("The product '{0}' version '{1}' with Id '{2}' checked for updates at {3}.", productName, currentVersion, productId, DateTime.Now.ToString()));
				
				// there must be a path to the updates folder
				if (updatesPath == null || updatesPath == string.Empty)
					return null;

				// append the product name to the updates path 
				string path = Path.Combine(updatesPath, productName);

				// if the directory doesn't exist, bail with null
				if (!Directory.Exists(path))
					return null;
											
				// create a search for manifest files
				Search search = new Search("Manifest Files", path, "*.Manifest", false, false);

				// run the search
				FileInfo[] files = search.GetFiles();

				// create versioned files from the results
				VersionedFile[] versionedFiles = VersionedFile.CreateVersionedFiles(string.Format("{0}-", productName), files);

				// sort them
				versionedFiles = VersionedFile.Sort(versionedFiles);

				// grab the latest version
				VersionedFile latestVersion = VersionedFile.GetLatestVersion(versionedFiles);
				
				// assuming there is a version available
				if (latestVersion != null)
				{					
					// create a new xml document to hold the response
					XmlDocument doc = new XmlDocument();

					// load the document with the xml
					doc.Load(latestVersion.File.FullName);

					// return the doc, which will return the document element, 
					// which is the pure xml inside the soap headers of the web service response
					// skipping anymore encoding issues.
					return doc;				
				}				
			}
			catch(Exception ex)
			{
				Trace.WriteLine(ex);
			}
			return null;
		}		
//		/// <summary>
//		/// Finds and creates all SnapIn classes discovered using the specified search.
//		/// </summary>
//		/// <param name="search"></param>
//		/// <param name="outputWindow"></param>
//		/// <returns></returns>
//		public static SnapInInfoCollection FindAndCreateSnapIns(Search search, IProgressViewer progressViewer)
//		{
//			SnapInInfoCollection snapIns = new SnapInInfoCollection();
//			try
//			{								
//				AppDomain domain = AppDomain.CreateDomain(Guid.NewGuid().ToString());
//				SnapInProvider provider = (SnapInProvider)domain.CreateInstanceFromAndUnwrap(System.Reflection.Assembly.GetExecutingAssembly().Location, typeof(SnapInProvider).FullName);
//				ArrayList array = provider.SearchForTypes(search, typeof(ISnapIn), progressViewer);
//				AppDomain.Unload(domain);
//
//				if (array != null)
//				{
//					foreach(RuntimeClassProviderEventArgs e in array)
//					{
//						object runTimeObject = SnapInProvider.CreateSnapInFrom(e.Assembly, e.Type, progressViewer);
//						if (runTimeObject != null)
//						{
//							snapIns.Add(e.Assembly, e.Type, (ISnapIn)runTimeObject);						
//							
////							if (outputWindow != null)
////								outputWindow.Write("Created SnapIn...Created SnapIn '" + e.Type.FullName + "'"); // from '" + e.Assembly.Location + "'");
////	
////							System.Diagnostics.Trace.WriteLine("Created SnapIn '" + e.Type.FullName + "' from '" + e.Assembly.Location + "'", typeof(SnapInProvider).Name);
//						}
//					}
//				}
//			}
//			catch(System.Exception systemException)
//			{
//				System.Diagnostics.Trace.WriteLine(systemException);
//			}
//			return snapIns;
//		}

		public static ArrayList FindSnapIns(Search search, IProgressViewer progressViewer)
		{
			try
			{
				AppDomain domain = AppDomain.CreateDomain(Guid.NewGuid().ToString());
				SnapInProvider provider = (SnapInProvider)domain.CreateInstanceFromAndUnwrap(System.Reflection.Assembly.GetExecutingAssembly().Location, typeof(SnapInProvider).FullName);
				ArrayList array = provider.SearchForTypes(search, typeof(ISnapIn), progressViewer);
				AppDomain.Unload(domain);
				return array;
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
			return new ArrayList();
		}
예제 #4
0
		/// <summary>
		/// saves a search to a file
		/// </summary>
		/// <param name="search">the search object to serialize to the file</param>
		/// <param name="filename">the filename to save the search to</param>
		public static void Save(Search search, string filename)
		{
			// use the SerializationProvider to serialize the object to the file
			SerializationProvider.Serialize(search, FormatterTypes.Soap, filename);
		}
예제 #5
0
		/// <summary>
		/// loads a search from a file
		/// </summary>
		/// <param name="search">the search object to load with the serialized data</param>
		/// <param name="filename">the filename containing the serialized data to load</param>
		public static void Load(ref Search search, string filename)
		{
			// use the SerializationProvider to deserialize the object from the file
			search = (Search)SerializationProvider.Deserialize(FormatterTypes.Soap, filename);
		}
		public void Run(string[] args, System.Reflection.Assembly executable)
		{		
			bool tracedExceptionThrown = false;				
			try
			{	
				using (SplashWindowThread splashThread = new SplashWindowThread(executable, false))
				{
					splashThread.ShowAsynchronously();			
					splashThread.Window.SetMarqueeMoving(true, true);
					
					ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Parsing command line...");

					// create a new command line parsing engine
					CommandLineParsingEngine pe = new CommandLineParsingEngine(args);
					
					// determine if we are going to keep the old versions
					bool keepOld = pe.ToBoolean("keepold");

					// the process id of an app
					int pid = pe.ToInt32("pid");

					// whether we should wait on the specified pid to die before launching new version
					bool wait = pe.ToBoolean("wait");

					ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Searching for runnable version...");

					// create a search for all of the subdirectories
					Search search = new Search("Versions", Application.StartupPath, "*", false, false);

					// find all of the directories
					DirectoryInfo[] directories = search.GetDirectories();

					// create versioned files around each directory that can be parsed to a version
					VersionedDirectory[] versionedDirectories = this.CreateVersionedFiles(directories);

					// if we have been instructed to wait on the process specified by pid to die, do it now
					if (wait && pid != 0)
					{
						try
						{
							// snag it and wait on it to exit
							Process p = Process.GetProcessById(pid);
							if (p != null)
							{
								ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Closing previous instance...");
								p.WaitForExit();
							}
						}
						catch(System.Exception systemException)
						{
							System.Diagnostics.Trace.WriteLine(systemException);
						}
					}
				
					ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Selecting latest runnable version...");

					// try and start the newest version
					VersionedDirectory versionStarted;
					bool startedVersion = this.StartNewestVersion(executable, versionedDirectories, out versionStarted, splashThread.Window);
					// this will fall back upon older versions until it runs out of versions or one of the versions starts
					if (!startedVersion)
					{
						string exeName = string.Format("{0}.exe", executable.GetName().Name);
						ExceptionEngine.DisplayException(null, "BootStrap failed for " + exeName, MessageBoxIcon.Stop,  MessageBoxButtons.OK, null, 
							"No suitable executable was found or able to be started.");
					}

					// if we're not keeping the old versions
					if (!keepOld)
					{
						// delete the older versions 
						if (!this.DeleteOlderVersions(versionedDirectories, versionStarted, splashThread.Window))
						{
							// um, who cares if we can't delete the older versions
							// also we need to see about security rights to the directories
						}
					}
					
					// if we started a version
					if (startedVersion)
						// notify that we are transferring control now to it...
						ProgressViewer.SetExtendedDescription(splashThread.Window, "Bootstrap: Transferring control to version " + versionStarted.Version.ToString() + "...");					
				}
			}
			catch(System.Exception systemException)
			{
				tracedExceptionThrown = true;
				System.Diagnostics.Trace.WriteLine(systemException);
				System.Windows.Forms.MessageBox.Show(null, systemException.ToString(), "Application Exiting");
				Application.ExitThread();
			}
			finally
			{
				System.Diagnostics.Trace.WriteLine("'" + Application.ProductName + (tracedExceptionThrown ? "' has terminated because of an exception." : "' has exited gracefully."));
			}
		}
		/// <summary>
		/// Searches the application path for assemblies that contain SnapIns. Returns a collection of SnapIns that match our criteria.
		/// </summary>
		/// <param name="search">The search to execute</param>
		/// <param name="ignoreUninstalledVersions">A flag indicating whether we should ignore uninstalled versions of a SnapIn</param>
		/// <param name="progressViewer">The progress viewer by which we can report information about our progress</param>
		/// <returns></returns>
		public static SnapInDescriptor[] SearchForSnapIns(Search search, IProgressViewer progressViewer)
		{
			ArrayList descriptors = new ArrayList();
			try
			{
				ProgressViewer.SetExtendedDescription(progressViewer, "Searching assemblies for exported SnapIns...");

				// set the location algorithm, the quickest is the metadata algorithm
				SnapInProvider.LocationAlgorithm = SnapInLocationAlgorithms.LocateUsingMetadata;
				
				// find the types that constitute eligible SnapIns
				ArrayList snapInsFound = SnapInProvider.FindSnapIns(search, progressViewer);
				
				// iterate over the provided types and create those that suffice our search parameters
				foreach(RuntimeClassProviderEventArgs classProviderArgs in snapInsFound)
				{																			
					try
					{
						// create an instance of the type we found
						object instance = SnapInHostingEngine.CreateSnapIn(classProviderArgs.Type, progressViewer);
						if (instance != null)
						{		
							SnapInDescriptor descriptor = new SnapInDescriptor(classProviderArgs.Type, (ISnapIn)instance);

							// lookup the versions of this type that are uninstalled
							Version[] versions = InstallationEngine.GetUninstalledVersionsOfType(SnapInHostingEngine.GetExecutingInstance().InstallationEngineConfiguration, classProviderArgs.Type);
				
							// if the current version is included in the versions, then we should skip this one
							if (InstallationEngine.IsVersionOfTypeIncluded(versions, classProviderArgs.Type))
								descriptor._isUninstalled = true;
							
							descriptors.Add(descriptor);
						}
					}
					catch(System.Exception systemException)
					{
						System.Diagnostics.Trace.WriteLine(systemException);
					}					
				}
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
			return descriptors.ToArray(typeof(SnapInDescriptor)) as SnapInDescriptor[];
		}
		/// <summary>
		/// Deletes any old log files that occur outside of the max log files setting
		/// </summary>
		private void DeleteOldLogFiles()
		{
			/*
			 * Log File Naming Convention
			 * 
			 * string.Format("Log{0}.txt", number);
			 * 
			 * */

			int maxFiles = DefaultMaxLogFiles;
			string pattern = DefaultLogFileSearchingPattern;

			try
			{
				// create a search for files that fit the log file naming scheme
				Search search = new Search("Log Files", _logsDataPath, pattern, false, true);
				
				// search for the files
				FileInfo[] files = search.GetFiles();
				
				foreach(FileInfo file in files)
				{
					try
					{
						// break the file name down into just the number of the log file
						string name = file.Name;
						name = name.Replace("Log_", null);
						name = name.Replace(file.Extension, null);

						// try and figure out the number of the log file
						int number = int.Parse(name);

						// try and delete any file that is a log file backup greater that the max number of log files
						if (number > maxFiles)
							this.FileDelete(file.FullName);
					}
					catch(FormatException)
					{
						continue;
					}
					catch(Exception ex)
					{
						Debug.WriteLine(ex);
					}
				}
			}
			catch(Exception ex)
			{
				Debug.WriteLine(ex);
			}
		}