You are currently viewing Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#
Associating a File Type with the Window Form Application

Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#

Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#.

Step 1: Create New Window Form Application.

Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#
Open New Project Window in Visual Studio 2017

Step 2: Add new form – Form2 (You can change the Form2 Name if you want)

Step 3: In the constructor for Form2 add a string parameter. This will be the filename of the file clicked.

Step 4: Add RichTextBox to Form2 and Implement Following code within Form2 Constructor. This will read file and displat text in RichTextBox.

public partial class Form2 : Form

    {

        public Form2(string fileName)

        {

            String line;

            InitializeComponent();

            try

            {

                StreamReader sr = new StreamReader(fileName);

                do

                {

                    line = sr.ReadLine();

                    if (line != null)

                    {

                        richTextBox1.AppendText(line + “\n”);

                    }

                } while (line != null);

                sr.Close();

            }

            catch (Exception e)

            {

                Console.WriteLine(“Exception: ” + e.Message);

            }

        }

    }

Step 4: Now open Program.cs file and change the following code. Here I added args as argument with Main Method. Program.cs Main Method automatically detect the File path when click on the custom type file (Or Open any file with this application) and pass as argument to Form2.

static class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main(string[] args)

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            if(args.Length==0)

                Application.Run(new Form1());

            else

                Application.Run(new Form2(args[0]));

        }

    }

Step 5: Rebuild the application and generate exe file.

Now our next step is creating setup project for the same application.

Step 6: Add New Setup Project to your current solution project.

Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#
Add New Setup Project

Step 7: Add Your Project Primary output to Application Folder. You can also add ICO for the custom file.

Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#
Add Primary Output to Application Folder

Step 8: Right Click on your Setup project and select View -> File Types

Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#
Select View -> File Type

Step 9: Now right click on “File Types on Target Machine” -> Add File Type.

Step 10: Select New Custom File Type and go to property window (F4) and Set the Name, Description, Extensions, Command To your primary output and Custom ICO file.

Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#
Set Properties For Custom File Type

Step 11: Select “Open” -> Go to property and set argument as follow.

Associating a File Type with the Window Form Application and Open Custom File with the Window Form Application in C#
Set Properties for Open Option

Step 12: Rebuild the Setup Project as Release. This will create new EXE file. You can Install this new EXE file.

Step 13: After installation you can open your custom file type with your application. Here .pdoc is our custom file type.

Note: If your custom file ICO not set to your custom ICO then you can change File open with to your custom Application.

Hope this will helps you lots.