File Association for application

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 botsokoy  Icon User is offline

  • New D.I.C Head

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

File Association for application

Posted 03 October 2012 - 09:04 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. 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 of 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.

Is This A Good Question/Topic? 0
  • +

Replies To: File Association for application

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: File Association for application

Posted 03 October 2012 - 09:58 PM

I can't say for sure but there sure looks like an issue between lines 11 and 13 in the sample 2. Only if the args.Length==0 will application.run be called and the empty parameters will be passed to mainform. You need a way to stop this, by only passing the variable to mainform by checking first if args.Length != 0.
Before I go on any further, what exactly is the bug?
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: File Association for application

Posted 03 October 2012 - 10:14 PM

DimitriV is correct.

If you look closely at the source code in the link you provided, if the number of arguments is zero, he opens Form1 without any parameters, otherwise, he opens Form2 passing in the first commandline parameter:
if (args.Length == 0) Application.Run(new Form1())
else                  Application.Run(new Form2(args[0]));


Was This Post Helpful? 0
  • +
  • -

#4 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 04 October 2012 - 12:38 AM

View PostDimitriV, on 03 October 2012 - 09:58 PM, said:

I can't say for sure but there sure looks like an issue between lines 11 and 13 in the sample 2. Only if the args.Length==0 will application.run be called and the empty parameters will be passed to mainform. You need a way to stop this, by only passing the variable to mainform by checking first if args.Length != 0.
Before I go on any further, what exactly is the bug?

The bug is the external file cannot be access into my application. Maybe my codes have been lacking something and I don't know which part of it. I been trying to fix it in a couple of days but the result is still the same.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: File Association for application

Posted 04 October 2012 - 12:54 AM

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.
Was This Post Helpful? 0
  • +
  • -

#6 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 04 October 2012 - 12:56 AM

View PostSkydiver, on 03 October 2012 - 10:14 PM, said:

DimitriV is correct.

If you look closely at the source code in the link you provided, if the number of arguments is zero, he opens Form1 without any parameters, otherwise, he opens Form2 passing in the first commandline parameter:
if (args.Length == 0) Application.Run(new Form1())
else                  Application.Run(new Form2(args[0]));


Ok thanks for that. Now, I make a new form which I called MainForm2 in which the argument would be pass in. But, still the argument that would be pass into the Mainform2 is empty or it cannot be access. I think the process is correct but I lack something for my code.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: File Association for application

Posted 04 October 2012 - 05:46 AM

Are you trying to debug the actions of the command line parameters via the IDE? Have you set the command line parameters to pass it in the project properties?
Was This Post Helpful? 0
  • +
  • -

#8 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 04 October 2012 - 11:30 PM

View PostAdamSpeight2008, on 04 October 2012 - 05:46 AM, said:

Are you trying to debug the actions of the command line parameters via the IDE? Have you set the command line parameters to pass it in the project properties?

Yes, I was trying to debug the actions of the command line into the IDE. I also add a primary output which is an .exe file to my command line for my installer.
Was This Post Helpful? 0
  • +
  • -

#9 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 04 October 2012 - 11:36 PM

View PostAdamSpeight2008, on 04 October 2012 - 05:46 AM, said:

Are you trying to debug the actions of the command line parameters via the IDE? Have you set the command line parameters to pass it in the project properties?

Yes, I was trying to debug the actions of the command line into the IDE. I also add a primary output which is an .exe file to my command line for my installer.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: File Association for application

Posted 04 October 2012 - 11:42 PM

So does the value of args[0] match the string that you put in the Project Properties page's command line arguments field?
Was This Post Helpful? 0
  • +
  • -

#11 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 04 October 2012 - 11:44 PM

View PostAdamSpeight2008, on 04 October 2012 - 05:46 AM, said:

Are you trying to debug the actions of the command line parameters via the IDE? Have you set the command line parameters to pass it in the project properties?

Yes, I was trying to debug the command line parameters via IDE. I also put a primary output into the command line for my installer. But the file from the external path cannot be read into my application. I think I missed something into my program codes.
Was This Post Helpful? 0
  • +
  • -

#12 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 04 October 2012 - 11:50 PM

View PostAdamSpeight2008, on 04 October 2012 - 05:46 AM, said:

Are you trying to debug the actions of the command line parameters via the IDE? Have you set the command line parameters to pass it in the project properties?

Yes, I was trying to debug the command line parameters via IDE. I also put a primary output into the command line for my installer. But the file from the external path cannot be read into my application. I think I missed something into my program codes. I follow the instructions from this blog http://social.msdn.m...b2-3da029b1cd46.

View PostAdamSpeight2008, on 04 October 2012 - 05:46 AM, said:

Are you trying to debug the actions of the command line parameters via the IDE? Have you set the command line parameters to pass it in the project properties?

Yes, I was trying to debug the command line parameters via IDE. I also put a primary output into the command line for my installer. But the file from the external path cannot be read into my application. I think I missed something into my program codes. I follow the instructions from this blog http://social.msdn.m...b2-3da029b1cd46.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: File Association for application

Posted 05 October 2012 - 06:40 AM

You need to add your command line arguments.
Attached Image
Was This Post Helpful? 0
  • +
  • -

#14 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 05 October 2012 - 01:40 PM

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.

When I double click the file that I want to open, it is still the same. The file which I double click did not display into my datagridview.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: File Association for application

Posted 05 October 2012 - 02:15 PM

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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2