Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C# 4.0

Quick XML Reader

Rate me:
Please Sign up or sign in to vote.
4.78/5 (18 votes)
23 Feb 2011CPOL4 min read 85.3K   14.9K   72  
A quick XML interpreter for large XML files.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Trezorix.Qxr.Forms;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Trezorix.Qxr
{
	static class Program
	{

		// Extern DLL for sending a message to the running software instance
		[DllImport("User32.dll", EntryPoint = "SendMessage")]
		public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);

		// Extern DLL for setting the focus on the running software
		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		static extern bool SetForegroundWindow(IntPtr hWnd);

		// Constant for sending data between software instances
		internal const int WM_COPYDATA = 0x4A;

		// Structure containing data sent to other software instances
		public struct COPYDATASTRUCT
		{
			public IntPtr dwData;
			public int cbData;
			[MarshalAs(UnmanagedType.LPStr)]
			public string lpData;
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] arguments)
		{

			string m_sFileToOpen = null;

			foreach (string fileToOpen in arguments)
			{
				if (File.Exists(fileToOpen))
					m_sFileToOpen = fileToOpen;
			}


			// Get the running instances of the system
			Process proc = Process.GetCurrentProcess();
			Process[] processes = Process.GetProcessesByName(proc.ProcessName);

			// If there is a different instance of the system running, pass
			// the filename command to the running version of the software
			// and focus that window
			if (processes.Length > 1)
			{
				//iterate through all running target applications
				foreach (Process p in processes)
				{
					if (p.Id != proc.Id)
					{
						int result = 0;

						// Get a pointer to the main window of the already running version
						IntPtr targetWindow = p.MainWindowHandle;

						// Send the file opened to the other running instance
						if (!string.IsNullOrWhiteSpace(m_sFileToOpen))
						{
							byte[] sarr = System.Text.Encoding.Default.GetBytes(m_sFileToOpen);
							int len = sarr.Length;
							COPYDATASTRUCT cds;
							cds.dwData = (IntPtr)100;
							cds.lpData = m_sFileToOpen;
							cds.cbData = len + 1;
							result = SendMessage(targetWindow, WM_COPYDATA, IntPtr.Zero, ref cds);
						}

						// Focus the main window of the other instance
						SetForegroundWindow(p.MainWindowHandle);
					}
				}
			}
			else
			{
				Application.EnableVisualStyles();
				Application.SetCompatibleTextRenderingDefault(false);

				// This is the only running instance, create a new instance
				// of the main window and display it.
				using (Main frmMain = new Main())
				{
					if (!string.IsNullOrWhiteSpace(m_sFileToOpen))
						frmMain.OpenAtStartup.Add(m_sFileToOpen);
					Application.Run(frmMain);
				}
			}
		}

	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect http://4dotnet.nl
Netherlands Netherlands
I'm developing software for over two decades. I'm working as a cloud solution architect and a team lead at 4DotNet in The Netherlands.

In my current position, I love to help customers with their journey to the cloud. I like to create highly performant software and to help team members to a higher level of software development.

My focus is on the Microsoft development stack, mainly C# and the Microsoft Azure Cloud. I also have a strong affinity with Angular. As a result of my contributions to the community, I received the Microsoft MVP Award for Microsoft Azure.

Comments and Discussions