larynill ances

larynill ances

  • NA
  • 1
  • 1.5k

File Association for application

Oct 3 2012 7:01 PM
I have a program that will compress a file and give an output extension file of .pld. After compressing the file I just want to open it into my application without using the open dialog for windows form. I see some resource for file association for application. Here is the link  http://www.c-sharpcorner.com/uploadfile/71c973/associating-a-file-type-with-an-application-in-C-Sharp/. I wrote some code like that but there was a bug on it but I cant fix it. Here my code for my form:

public MainForm(string fN)
        {
            string filename = Path.GetFullPath(fN);
            string filePath = Path.GetFileName(fN);
            InitializeComponent();
            DataTable pldtable = new DataTable();
          
            //set datagridview columns
            pldtable.Columns.Add("Full Name");
            pldtable.Columns.Add("File Name");
            pldtable.Columns.Add("Compress Size");
            pldtable.Columns.Add("Readable Size");
            pldtable.Columns.Add("Date Modified");
            pldtable.Columns.Add("Uncompress Size");
            pldtable.Columns.Add("Type");

          
            if(fN.Length == 0)
            {
                MessageBox.Show("File don't Exist","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
           
            }
            else
            {
                tb.Text = filename;
                openStatus.Visible = true;
                using (ZipFile pld = ZipFile.Read(filePath))
                    {
                        foreach (ZipEntry en in pld)
                        {
                            openStatus.Text = string.Format("Reading: {0}", en.FileName);
                            openStatus.Update();
                            string fullFileName = en.FileName;
                            string extension;
                            extension = Path.GetExtension(fullFileName);
                            FileInfo fileInfo = new FileInfo(fullFileName);
                            string fileType = fileInfo.Name;
                          
                            //set bytes size

                            string sizeReadable = en.CompressedSize.ToString() + " bytes";
                            string resultDecomp = en.UncompressedSize.ToString() + " bytes";

                            //add filename to rows
                            pldtable.Rows.Add(en.FileName, fileType, sizeReadable, en.CompressedSize, en.LastModified, resultDecomp, extension);
                            dgv.DataSource = pldtable;
                            dgv.Columns[3].Visible = false;
                            dgv.Columns[0].Visible = false;
                            dgv.Columns[1].Width = 150;
                            dgv.Columns[2].Width = 120;
                            dgv.Columns[4].Width = 120;
                            dgv.Columns[5].Width = 130;
                            dgv.Columns[6].Width = 63;
                            if (dgv.Rows.Count > 1)
                            {
                                int total = 0;
                                for (int i = 0; i < dgv.RowCount; i++)
                                {
                                    total += Convert.ToInt32(dgv.Rows[i].Cells[3].Value);
                                    totalSize.Text = total.ToString() + " bytes";
                                    totalFiles.Text = dgv.Rows[i].Index.ToString() + " files";
                                }
                            }
                    }
                }

            }
           
            dgv.Sort(dgv.Columns[3], ListSortDirection.Descending);
            openStatus.Visible = false;
            appStat.Text = "Success opening file.";
        }


And here is the code for my program.cs:

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 MainForm(args[0]));
        }
    }

And in my file types I put on to the command as primary output of the file.