3 Replies - 4178 Views - Last Post: 25 December 2009 - 02:25 PM Rate Topic: -----

#1 apease11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-July 08

Opening Files With My Application

Posted 25 December 2009 - 12:20 PM

Hi, I am working on a project that can save and open files. I have those functions almost perfected at this point in time. What I would like to do, is have you be able to double-click on the file my program creates in Windows Explorer and have my application launch and open it.

I am using Visual C# and my "Program.cs" script looks like this:
static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			Application.Run(new Form1());
		}

I know I have to put something in there to catch what the system is throwing at it, but don't know what. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Opening Files With My Application

#2 JustCute  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 25-December 09

Re: Opening Files With My Application

Posted 25 December 2009 - 01:49 PM

File Association huh?
using System;
using System.Security;
using System.Collections;
using Microsoft.Win32;

// The Org.Mentalis.Utilities namespace implements several useful utilities that are missing from the standard .NET framework.
namespace Org.Mentalis.Utilities {
	/// <summary>List of commands.</summary>
	internal struct CommandList {
		/// <summary>
		/// Holds the names of the commands.
		/// </summary>
		public ArrayList Captions;
		/// <summary>
		/// Holds the commands.
		/// </summary>
		public ArrayList Commands;
	}
	/// <summary>Properties of the file association.</summary>
	internal struct FileType {
		/// <summary>
		/// Holds the command names and the commands.
		/// </summary>
		public CommandList Commands;
		/// <summary>
		/// Holds the extension of the file type.
		/// </summary>
		public string Extension;
		/// <summary>
		/// Holds the proper name of the file type.
		/// </summary>
		public string ProperName;
		/// <summary>
		/// Holds the full name of the file type.
		/// </summary>
		public string FullName;
		/// <summary>
		/// Holds the name of the content type of the file type.
		/// </summary>
		public string ContentType;
		/// <summary>
		/// Holds the path to the resource with the icon of this file type.
		/// </summary>
		public string IconPath;
		/// <summary>
		/// Holds the icon index in the resource file.
		/// </summary>
		public short IconIndex;
	}
	/// <summary>Creates file associations for your programs.</summary>
	/// <example>The following example creates a file association for the type XYZ with a non-existent program.
	/// <br></br><br>VB.NET code</br>
	/// <code>
	/// Dim FA as New FileAssociation
	/// FA.Extension = "xyz"
	/// FA.ContentType = "application/myprogram"
	/// FA.FullName = "My XYZ Files!"
	/// FA.ProperName = "XYZ File"
	/// FA.AddCommand("open", "C:\mydir\myprog.exe %1")
	/// FA.Create
	/// </code>
	/// <br>C# code</br>
	/// <code>
	/// FileAssociation FA = new FileAssociation();
	/// FA.Extension = "xyz";
	/// FA.ContentType = "application/myprogram";
	/// FA.FullName = "My XYZ Files!";
	/// FA.ProperName = "XYZ File";
	/// FA.AddCommand("open", "C:\\mydir\\myprog.exe %1");
	/// FA.Create();
	/// </code>
	/// </example>
	public class FileAssociation {
		/// <summary>Initializes an instance of the FileAssociation class.</summary>
		public FileAssociation() {
			FileInfo = new FileType();
			FileInfo.Commands.Captions = new ArrayList();
			FileInfo.Commands.Commands = new ArrayList();
		}
		/// <summary>Gets or sets the proper name of the file type.</summary>
		/// <value>A String representing the proper name of the file type.</value>
		public string ProperName {
			get {
				return FileInfo.ProperName;
			}
			set {
				FileInfo.ProperName = value;
			}
		}
		/// <summary>Gets or sets the full name of the file type.</summary>
		/// <value>A String representing the full name of the file type.</value>
		public string FullName {
			get {
				return FileInfo.FullName;
			}
			set {
				FileInfo.FullName = value;
			}
		}
		/// <summary>Gets or sets the content type of the file type.</summary>
		/// <value>A String representing the content type of the file type.</value>
		public string ContentType {
			get {
				return FileInfo.ContentType;
			}
			set {
				FileInfo.ContentType = value;
			}
		}
		/// <summary>Gets or sets the extension of the file type.</summary>
		/// <value>A String representing the extension of the file type.</value>
		/// <remarks>If the extension doesn't start with a dot ("."), a dot is automatically added.</remarks>
		public string Extension {
			get {
				return FileInfo.Extension;
			}
			set {
				if (value.Substring(0, 1) != ".")
					value = "." + value;
				FileInfo.Extension = value;
			}
		}
		/// <summary>Gets or sets the index of the icon of the file type.</summary>
		/// <value>A short representing the index of the icon of the file type.</value>
		public short IconIndex {
			get {
				return FileInfo.IconIndex;
			}
			set {
				FileInfo.IconIndex = value;
			}
		}
		/// <summary>Gets or sets the path of the resource that contains the icon for the file type.</summary>
		/// <value>A String representing the path of the resource that contains the icon for the file type.</value>
		/// <remarks>This resource can be an executable or a DLL.</remarks>
		public string IconPath {
			get {
				return FileInfo.IconPath;
			}
			set {
				FileInfo.IconPath = value;
			}
		}
		/// <summary>Adds a new command to the command list.</summary>
		/// <param name="Caption">The name of the command.</param>
		/// <param name="Command">The command to execute.</param>
		/// <exceptions cref="ArgumentNullException">Caption -or- Command is null (VB.NET: Nothing).</exceptions>
		public void AddCommand(string Caption, string Command) {
			if (Caption == null || Command == null)
				throw new ArgumentNullException();
			FileInfo.Commands.Captions.Add(Caption);
			FileInfo.Commands.Commands.Add(Command);
		}
		/// <summary>Creates the file association.</summary>
		/// <exceptions cref="ArgumentNullException">Extension -or- ProperName is null (VB.NET: Nothing).</exceptions>
		/// <exceptions cref="ArgumentException">Extension -or- ProperName is empty.</exceptions>
		/// <exceptions cref="SecurityException">The user does not have registry write access.</exceptions>
		public void Create() {
			// remove the extension to avoid incompatibilities [such as DDE links]
			try {
				Remove();
			} catch (ArgumentException) {} // the extension doesn't exist
			// create the exception
			if (Extension == "" || ProperName == "")
				throw new ArgumentException();
			int cnt;
			try {
				RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(Extension);
				RegKey.SetValue("", ProperName);
				if (ContentType != null && ContentType != "")
					RegKey.SetValue("Content Type", ContentType);
				RegKey.Close();
				RegKey = Registry.ClassesRoot.CreateSubKey(ProperName);
				RegKey.SetValue("", FullName);
				RegKey.Close();
				if (IconPath != "") {
					RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "DefaultIcon");
					RegKey.SetValue("", IconPath + "," + IconIndex.ToString());
					RegKey.Close();
				}
				for (cnt = 0; cnt < FileInfo.Commands.Captions.Count; cnt++) {
					RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "Shell" + "\\" + (String)FileInfo.Commands.Captions[cnt]);
					RegKey = RegKey.CreateSubKey("Command");
					RegKey.SetValue("", FileInfo.Commands.Commands[cnt]);
					RegKey.Close();
				}
			} catch {
				throw new SecurityException();
			}
		}
		/// <summary>Removes the file association.</summary>
		/// <exceptions cref="ArgumentNullException">Extension -or- ProperName is null (VB.NET: Nothing).</exceptions>
		/// <exceptions cref="ArgumentException">Extension -or- ProperName is empty -or- the specified extension doesn't exist.</exceptions>
		/// <exceptions cref="SecurityException">The user does not have registry delete access.</exceptions>
		public void Remove() {
			if (Extension == null || ProperName == null)
				throw new ArgumentNullException();
			if (Extension == "" || ProperName == "")
				throw new ArgumentException();
			Registry.ClassesRoot.DeleteSubKeyTree(Extension);
			Registry.ClassesRoot.DeleteSubKeyTree(ProperName);
		}
		/// <summary>Holds the properties of the file type.</summary>
		private FileType FileInfo;
}
}


There is no copyright law on this code. (Got it from somewhere)
Was This Post Helpful? 1
  • +
  • -

#3 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Opening Files With My Application

Posted 25 December 2009 - 02:19 PM

Take a look at this article on system file association:
http://www.codeproje...ssociation.aspx
Was This Post Helpful? 1
  • +
  • -

#4 apease11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-July 08

Re: Opening Files With My Application

Posted 25 December 2009 - 02:25 PM

Thank you, although I just figured it out myself.

public static void Main(string[] args)
		{
			string path = "";
			//pplication.EnableVisualStyles();
			if (args.Length > 0)
			{
				path = Convert.ToString(args[0]);
			}
			
			Application.SetCompatibleTextRenderingDefault(false);

			Application.Run(new Form1(path));
		}

public Form1(string path)
		{
			...Other Code...
			if (path == "") { }
			else
			{
				 loadXMLInformation(path, null, null);
			}
		}


And I set up my registry manually, that was what I was truly stuck on.

[HKEY_CLASSES_ROOT\Halo.Firefight.Recorder]
@="Halo Firefight Recorder Document"

[HKEY_CLASSES_ROOT\Halo.Firefight.Recorder\shell]
@="Open"

[HKEY_CLASSES_ROOT\Halo.Firefight.Recorder\shell\open]
@="&Open"

[HKEY_CLASSES_ROOT\Halo.Firefight.Recorder\shell\open\command]
@="\"F:\\Documents and Settings\\Alden\\Local Settings\\Apps\\2.0\\KPL05XBA.C4O\\DXQ3DWMK.PVH\\fire..tion_1c5a688fe8a8fbaa_0001.0002_4b09bdee8662e58c\\Firefight Logger.exe\" \"%1\""

[HKEY_CLASSES_ROOT\.hfr]
"Content Type"="application/xml"
@="Halo.Firefight.Recorder"

Once I finish the application, I will give it a solid application path and make an actual installer that does not run off of ClickOnce to add the registry values automatically.

Thank you for your help though, I may use that code for future projects.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1