Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / C#

Opening multiple document windows with previous instance

Rate me:
Please Sign up or sign in to vote.
3.67/5 (9 votes)
15 Mar 20042 min read 69.6K   1.2K   19  
Communicates with previous instance by passing command line argument file name. Previous instance opens new child window each time file name is passed.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace CodeProject.Sample
{
	/// <summary>
	/// Summary description for FrmTestHarness.
	/// </summary>
	public class FrmTestHarness : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtArgs;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtExe;
		private System.Windows.Forms.Label lblRun;
		private System.Windows.Forms.Button btnRun;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FrmTestHarness()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FrmTestHarness());
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtArgs = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtExe = new System.Windows.Forms.TextBox();
			this.lblRun = new System.Windows.Forms.Label();
			this.btnRun = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 104);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(160, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Command Line Arguments:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
			// 
			// txtArgs
			// 
			this.txtArgs.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.txtArgs.Location = new System.Drawing.Point(24, 128);
			this.txtArgs.Name = "txtArgs";
			this.txtArgs.Size = new System.Drawing.Size(352, 20);
			this.txtArgs.TabIndex = 1;
			this.txtArgs.Text = "FileName.ext";
			this.txtArgs.TextChanged += new System.EventHandler(this.txtArgs_TextChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(160, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Executable:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
			// 
			// txtExe
			// 
			this.txtExe.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.txtExe.Location = new System.Drawing.Point(24, 80);
			this.txtExe.Name = "txtExe";
			this.txtExe.Size = new System.Drawing.Size(352, 20);
			this.txtExe.TabIndex = 3;
			this.txtExe.Text = "";
			this.txtExe.TextChanged += new System.EventHandler(this.txtExe_TextChanged);
			// 
			// lblRun
			// 
			this.lblRun.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.lblRun.Location = new System.Drawing.Point(32, 176);
			this.lblRun.Name = "lblRun";
			this.lblRun.Size = new System.Drawing.Size(344, 40);
			this.lblRun.TabIndex = 4;
			// 
			// btnRun
			// 
			this.btnRun.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
			this.btnRun.Location = new System.Drawing.Point(156, 232);
			this.btnRun.Name = "btnRun";
			this.btnRun.Size = new System.Drawing.Size(72, 24);
			this.btnRun.TabIndex = 5;
			this.btnRun.Text = "Run";
			this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 152);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(160, 23);
			this.label3.TabIndex = 6;
			this.label3.Text = "Run Path:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(24, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(344, 32);
			this.label4.TabIndex = 7;
			this.label4.Text = "Make sure to copy SingleInstance.exe to same directory as this application.";
			this.label4.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
			// 
			// FrmTestHarness
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(384, 270);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label4,
																		  this.label3,
																		  this.btnRun,
																		  this.lblRun,
																		  this.txtExe,
																		  this.label2,
																		  this.txtArgs,
																		  this.label1});
			this.Name = "FrmTestHarness";
			this.Text = "Single Instance Test Harness";
			this.Load += new System.EventHandler(this.FrmTestHarness_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnRun_Click(object sender, System.EventArgs e)
		{
			Process.Start( txtExe.Text, txtArgs.Text );
		}

		private void FrmTestHarness_Load(object sender, System.EventArgs e)
		{
			string path = Path.GetDirectoryName(Application.ExecutablePath);
			if ( path[path.Length - 1] != Path.DirectorySeparatorChar )
				path += Path.DirectorySeparatorChar;
			path += "SingleInstance.exe";
			if ( File.Exists( path ) )
				txtExe.Text = path;
		}

		private void txtExe_TextChanged(object sender, System.EventArgs e)
		{
			lblRun.Text = txtExe.Text + " \"" + txtArgs.Text + "\"";
		}

		private void txtArgs_TextChanged(object sender, System.EventArgs e)
		{
			lblRun.Text = txtExe.Text + " \"" + txtArgs.Text + "\"";
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions