示例#1
0
 public static VersionedDirectory[] Sort(VersionedDirectory[] directories)
 {
     // front to back - 1
     for (int i = 0; i < directories.Length - 1; i++)
     {
         // front + 1 to back
         for (int j = i + 1; j < directories.Length; j++)
         {
             if (directories[i].Version < directories[j].Version)
             {
                 // swap i with j, where i=1 and j=2
                 VersionedDirectory directory = directories[j];
                 directories[j] = directories[i];
                 directories[i] = directory;
             }
         }
     }
     return(directories);
 }
		public static VersionedDirectory[] Sort(VersionedDirectory[] directories)
		{
			// front to back - 1 
			for(int i = 0; i < directories.Length - 1; i++)
			{
				// front + 1 to back
				for(int j = i + 1; j < directories.Length; j++)
				{			
					if (directories[i].Version < directories[j].Version)
					{											 
						// swap i with j, where i=1 and j=2
						VersionedDirectory directory = directories[j];
						directories[j] = directories[i];
						directories[i] = directory;
					}													
				}
			}
			return directories;
		}
		private bool DeleteOlderVersions(
			VersionedDirectory[] versionedDirectories,
			VersionedDirectory versionStarted,
			IProgressViewer progressViewer)
		{
			try
			{
				// grab the newest version
				VersionedDirectory newestVersion = versionedDirectories[0];

				// loop thru and delete the oldest versions not in use
				foreach(VersionedDirectory version in versionedDirectories)
				{
					// keep the newest version and the one that started
					if (version != newestVersion && version != versionStarted)
					{
						try
						{
							ProgressViewer.SetExtendedDescription(progressViewer, "Bootstrap: Removing older version " + version.Version.ToString() + "...");

							// recursively delete this version directory
							Directory.Delete(version.Directory.FullName, true);
						}
						catch(System.Exception systemException)
						{
							System.Diagnostics.Trace.WriteLine(systemException);
						}
					}
				}
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
			return false;
		}
		/// <summary>
		/// Attempts to start the newest version of the executable named the same thing as the executing assembly
		/// </summary>
		/// <param name="versionedDirectories"></param>
		/// <param name="versionStarted"></param>
		/// <returns></returns>
		private bool StartNewestVersion(
			Assembly executable, 
			VersionedDirectory[] versionedDirectories, 
			out VersionedDirectory versionStarted,
			IProgressViewer progressViewer)
		{
			versionStarted = null;

			try
			{
				// get the name of this assembly which will be what we look for to start
				string assemblyName = string.Format("{0}.exe", executable.GetName().Name);
				
				// will start with the newest version because they are sorted
				foreach(VersionedDirectory versionToStart in versionedDirectories)
				{					
					// format the path to the version we are going to attempt to start
					string path = Path.Combine(versionToStart.Directory.FullName, assemblyName);

					bool started = false;
					
					// if the file exists
					if (File.Exists(path))
					{
						ProgressViewer.SetExtendedDescription(progressViewer, "Bootstrap: Starting version " + versionToStart.Version.ToString() + "...");

						Process p = new Process();
						p.StartInfo.FileName = path;
						p.StartInfo.Arguments = System.Environment.CommandLine;
						p.StartInfo.WorkingDirectory = versionToStart.Directory.FullName;
						p.StartInfo.WindowStyle = Process.GetCurrentProcess().StartInfo.WindowStyle;
						
						// try to start this version
						started = p.Start();
					}

					// if a version was started
					if (started)
					{						
						// keep track of which version we started
						versionStarted = versionToStart;
						return true;
					}
				}
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
			return false;
		}
		/// <summary>
		/// Parses the names of the directories and creates a VersionedDirectory for each directory whose name could be parsed into a Version
		/// </summary>
		/// <param name="directories"></param>
		/// <returns></returns>
		private VersionedDirectory[] CreateVersionedFiles(DirectoryInfo[] directories)
		{
			try
			{
				ArrayList array = new ArrayList();
				foreach(DirectoryInfo directory in directories)
				{
					try
					{
						Version version = new Version(directory.Name);
						VersionedDirectory versionedFile = new VersionedDirectory(version, directory);
						array.Add(versionedFile);
					}
					catch
					{

					}
				}
				
				VersionedDirectory[] versionedFiles = array.ToArray(typeof(VersionedDirectory)) as VersionedDirectory[];
				versionedFiles = VersionedDirectory.Sort(versionedFiles);
				return versionedFiles;
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
			return null;
		}