File Association for application

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1220 Views - Last Post: 07 October 2012 - 11:16 PM Rate Topic: -----

#16 botsokoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-June 12

Re: File Association for application

Posted 06 October 2012 - 03:48 AM

View PostSkydiver, on 04 October 2012 - 12:54 AM, said:

What is the exact error, and an which line is it being reported on.

Could you also post your real code. The code you posted above will only call MainForm() if there are no command line parameters, yet you are passing in args[0]. You should have gotten an IndexOutOfRangeException if that were your real code.


program.cs
 static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length == 0)
            {
                Application.Run(new MainForm());
            }
            else 
            {
                Application.Run(new MainForm2(args));
            }
            
        }

MainForm2.cs
        using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
using Ionic.Zip;
using Ionic.Zlib;

namespace FileCompressionandDecompression
{
    public partial class MainForm2 : Form
    {

        public MainForm2(string[] args)
        {
            string filename = Path.GetFullPath(args[0]);
            string filePath = Path.GetFileName(args[0]);
            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 (args.Length == 0)
            {
                MessageBox.Show("File don't Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //enable compress button
                //compress files and put it to datagridview
            }
            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;
                        string fileName;
                        extension = Path.GetExtension(fullFileName);
                        fileName = Path.GetFileName(fullFileName);
                        FileInfo fileInfo = new FileInfo(fullFileName);
                        string fileType = fileInfo.Name;
                        DirectoryInfo dir = Directory.GetParent(fullFileName);
                        string folderName = dir.Parent + "//" + fileName;

                        //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.";
        }

        public static TextBox tb = new TextBox();
        public static DataGridView dgv = new DataGridView();
        public static ToolStripButton removebtn = new ToolStripButton();
        public static ToolStripButton decompbtn = new ToolStripButton();
        public static ToolStripButton decompbtnHere = new ToolStripButton();
        public static ToolStripLabel appStat = new ToolStripLabel();
        public static Label totalFiles = new Label();
        public static Label totalSize = new Label();
        public static Label labelSize = new Label();
        public static Label openStatus = new Label();

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open File";
            openFileDialog1.Filter = "Compressed Files (*.pld,*.zip)|*.pld;*.zip";
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            
            string sFilePath;
            sFilePath = openFileDialog1.FileName;
            string path = sFilePath;
            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 (sFilePath == "")
                return;

            if (System.IO.File.Exists(sFilePath) == false)
            {
                return;
            }
            else
            {
                textBox1.Text = sFilePath;
                label5.Visible = true;
            }

            //enable compress button
            toolStripButton4.Enabled = true;
            toolStripButton2.Enabled = true;
            toolStripButton6.Enabled = true;
            //compress files and put it to datagridview
            using (ZipFile pld = ZipFile.Read(sFilePath))
            {
                foreach (ZipEntry en in pld)
                {
                    label5.Text = string.Format("Reading: {0}", en.FileName);
                    label5.Update();
                    string fullFileName = en.FileName;
                    string extension;
                    string fileName;
                    extension = Path.GetExtension(fullFileName);
                    fileName = Path.GetFileName(fullFileName);
                    FileInfo fileInfo = new FileInfo(fullFileName);
                    string fileType = fileInfo.Name;
                    DirectoryInfo dir = Directory.GetParent(fullFileName);
                    string folderName = dir.Parent + "//" + fileName;
                    // resultPath = Path.GetDirectoryName(fullFileName);

                    //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);
                    dataGridView1.DataSource = pldtable;
                    dataGridView1.Columns[3].Visible = false;
                    dataGridView1.Columns[0].Visible = false;
                    dataGridView1.Columns[1].Width = 150;
                    dataGridView1.Columns[2].Width = 120;
                    dataGridView1.Columns[4].Width = 120;
                    dataGridView1.Columns[5].Width = 130;
                    dataGridView1.Columns[6].Width = 63;
                    if (dataGridView1.Rows.Count > 1)
                    {
                        int total = 0;
                        for (int i = 0; i < dataGridView1.RowCount; i++)
                        {
                            total += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
                            TotalSize.Text = total.ToString() + " bytes";
                            TotalFiles.Text = dataGridView1.Rows[i].Index.ToString() + " files";
                        }
                    }
                }

            }

            dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Descending);
            label5.Visible = false;
            appStatus.Text = "Success opening file";
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            AddFile2 addFile = new AddFile2();
            addFile.ShowDialog();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open File";
            openFileDialog1.Filter = "Compressed Files (*.pld,*.zip)|*.pld;*.zip";
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            string sFilePath;
            sFilePath = openFileDialog1.FileName;
            string path = sFilePath;
            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 (sFilePath == "")
                return;

            if (System.IO.File.Exists(sFilePath) == false)
            {
                return;
            }
            else
            {
                textBox1.Text = sFilePath;
                label5.Visible = true;
            }

            //enable compress button
            toolStripButton4.Enabled = true;
            toolStripButton2.Enabled = true;
            toolStripButton6.Enabled = true;
            //compress files and put it to datagridview
            using (ZipFile pld = ZipFile.Read(sFilePath))
            {
                foreach (ZipEntry en in pld)
                {
                    label5.Text = string.Format("Reading: {0}", en.FileName);
                    label5.Update();
                    string fullFileName = en.FileName;
                    string extension;
                    string fileName;
                    extension = Path.GetExtension(fullFileName);
                    fileName = Path.GetFileName(fullFileName);
                    FileInfo fileInfo = new FileInfo(fullFileName);
                    string fileType = fileInfo.Name;
                    DirectoryInfo dir = Directory.GetParent(fullFileName);
                    string folderName = dir.Parent + "//" + fileName;
                    //resultPath = Path.GetDirectoryName(fullFileName);

                    //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);
                    dataGridView1.DataSource = pldtable;
                    dataGridView1.Columns[3].Visible = false;
                    dataGridView1.Columns[0].Visible = false;
                    dataGridView1.Columns[1].Width = 150;
                    dataGridView1.Columns[2].Width = 120;
                    dataGridView1.Columns[4].Width = 120;
                    dataGridView1.Columns[5].Width = 130;
                    dataGridView1.Columns[6].Width = 63;
                    if (dataGridView1.Rows.Count > 1)
                    {
                        int total = 0;
                        for (int i = 0; i < dataGridView1.RowCount; i++)
                        {
                            total += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
                            TotalSize.Text = total.ToString() + " bytes";
                            TotalFiles.Text = dataGridView1.Rows[i].Index.ToString() + " files";
                        }
                    }
                }

            }

            dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Descending);
            label5.Visible = false;
            appStatus.Text = "Success opening file.";
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            //decompress files with specific directory
            using (ZipFile pld = new ZipFile())
            {
                string pldTodecomp = textBox1.Text;
                string pathFile = pldTodecomp;
                string fileDir;
                fileDir = Path.GetDirectoryName(pathFile);
                
                using (ZipFile pld2 = ZipFile.Read(pldTodecomp))
                {
                    foreach (ZipEntry en in pld2)
                    {
                        en.Extract(fileDir,
                            ExtractExistingFileAction.OverwriteSilently);
                    }
                }
                MessageBox.Show("Decompress file is complete", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Text = string.Empty;
                dataGridView1.DataSource = string.Empty;
                toolStripButton4.Enabled = false;
                toolStripButton2.Enabled = false;
                toolStripButton6.Enabled = false;
                appStatus.Text = "Ready";
                TotalSize.Text = "0";
                TotalFiles.Text = "0";
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            DecompTo2 DecompFrm = new DecompTo2(this);
            DecompFrm.ShowDialog();
        }

        private void MainForm2_Load(object sender, EventArgs e)
        {

            toolStripButton4.Enabled = false;
            toolStripButton2.Enabled = false;
            toolStripButton6.Enabled = false;
            tb = textBox1;
            dgv = dataGridView1;
            removebtn = toolStripButton6;
            decompbtn = toolStripButton4;
            decompbtnHere = toolStripButton2;
            appStat = appStatus;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            totalFiles = TotalFiles;
            totalSize = TotalSize;
            labelSize = label2;
            openStatus = label5;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            string pathFile = textBox1.Text;
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                DialogResult conDelete = MessageBox.Show("Are you sure you want to delete file?", "Delete",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (conDelete == DialogResult.Yes)
                {
                    string toBeRemove = (dataGridView1.SelectedCells[0].Value.ToString());

                    using (ZipFile pld = ZipFile.Read(pathFile))
                    {

                        pld.RemoveEntry(toBeRemove);
                        pld.Save();

                    }
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                    if (dataGridView1.Rows.Count > 1)
                    {
                        int total = 0;
                        for (int i = 0; i < dataGridView1.RowCount; i++)
                        {
                            total += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
                            TotalSize.Text = total.ToString() + " bytes";
                            TotalFiles.Text = dataGridView1.Rows[i].Index.ToString() + " files";
                        }
                    }
                }
                else if (conDelete == DialogResult.No)
                {
                    return;
                }
            }
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,933
  • Joined: 05-May 12

Re: File Association for application

Posted 06 October 2012 - 08:39 AM

Thanks for posting the code.

In post #14, you reported the double clicking on the file just gave you an empty datagrid. Since you didn't report that a message box saying "Error: File don't exist." that would indicate two possibilities:
1. The code went to line 42 of MainForm2.cs; or
2. The code went to line 7 of Program.cs.

The only true way to find out is to attach a debugger. See my post #15 to see how to do that.
Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,933
  • Joined: 05-May 12

Re: File Association for application

Posted 06 October 2012 - 08:47 AM

AdamSpeight2008 and I have been encouraging you to set the command line arguments in the project settings to help with debugging, and I've also given you another option to debug by attaching to a running process. You seem to be ignoring these. Please just tell us if you are avoiding these because you have no interest in using a debugger and would rather just be running the code in a trial and error approach.

If you are thinking of software development as a career, in the modern world you have to learn how to use a debugger unless the only thing you want to develop is plain HTML with no Javascript. Yes, the approach of not using a debugger and just observing the behavior and putting in some strategic printf()'s or message boxes or commenting out portions of code may work, but your development velocity will be dramatically slower as compared to being at actually observe your program in action.
Was This Post Helpful? 0
  • +
  • -

#19 botsokoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-June 12

Re: File Association for application

Posted 07 October 2012 - 03:08 PM

View PostSkydiver, on 05 October 2012 - 02:15 PM, said:

You still didn't answer our question about where you said you were debugging in the IDE and if you were setting the command line arguments.

Anyway, since you've chosen to take a different tack on debugging your problem, we'll have to debug things the hard way:

In your Main() method, even before the Application.EnableVisualStyles() call, add:
MessageBox.Show("Attach to me");



Rebuild and re-install your application. After you deploy you program, copy the .PDB file into the same directory that has your .exe file.

Next, double click on your .pld file.


The message box should show up.

Start Visual Studio, and choose: Debug.Attach to Process... Is the process list, you should see your .EXE as one of the choices. Click "Attach". Visual Studio may ask you to point to where your source files are.

Next, set a breakpoint on your line right before where you check the args.Length.

Let your program run, and press OK on the message box. Your breakpoint should be hit. You can then inspect the args array variable to see what command line is being passed to you. You can further trace in, and or set breakpoints in the code where you are trying to open the file.

This one was the output after all:
args {string[0]} string[]
args.Length 0

FileCompressionandDecompression.exe!FileCompressionandDecompression.Program.Main(string[] args = {string[0]}) Line 23 C#

So, the string that have been pass into my MainForm2.cs is 0.
Was This Post Helpful? 0
  • +
  • -

#20 botsokoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-June 12

Re: File Association for application

Posted 07 October 2012 - 03:55 PM

View Postbotsokoy, on 07 October 2012 - 03:08 PM, said:

View PostSkydiver, on 05 October 2012 - 02:15 PM, said:

You still didn't answer our question about where you said you were debugging in the IDE and if you were setting the command line arguments.

Anyway, since you've chosen to take a different tack on debugging your problem, we'll have to debug things the hard way:

In your Main() method, even before the Application.EnableVisualStyles() call, add:
MessageBox.Show("Attach to me");



Rebuild and re-install your application. After you deploy you program, copy the .PDB file into the same directory that has your .exe file.

Next, double click on your .pld file.


The message box should show up.

Start Visual Studio, and choose: Debug.Attach to Process... Is the process list, you should see your .EXE as one of the choices. Click "Attach". Visual Studio may ask you to point to where your source files are.

Next, set a breakpoint on your line right before where you check the args.Length.

Let your program run, and press OK on the message box. Your breakpoint should be hit. You can then inspect the args array variable to see what command line is being passed to you. You can further trace in, and or set breakpoints in the code where you are trying to open the file.

This one was the output after all:
args {string[0]} string[]
args.Length 0

FileCompressionandDecompression.exe!FileCompressionandDecompression.Program.Main(string[] args = {string[0]}) Line 23 C#

So, the string that have been pass into my MainForm2.cs is 0.

I can figure out now my problem. My problem is in the MainForm2.cs. In reading the file into my datagridview.
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,933
  • Joined: 05-May 12

Re: File Association for application

Posted 07 October 2012 - 11:16 PM

Huh? If args.Length == 0, then MainForm2() shouldn't be called based on your code in post #16.

What is the value of args[0]?

This post has been edited by Skydiver: 07 October 2012 - 11:17 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2