[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 778
  • Last Modified:

c# addin for vs 2005

I have created an addin that has a form in it. I would like to have my addin be able to read what ever project code I am in...Is there a way to do that... For example....when I'm in a new project and run my addin it would bring a messagebox that show the new project Program.cs application.run form...


Example..

on any new projects the program.cs looks like this.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


I would like it so that when I hit my addin it would bring a messagebox that would say: Form1

or lets say I changed my program.cs to this...

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmtest());
        }
    }
}

the messagebox would say frmtest.


basically want my addin to tell me what application.run form is first..

is that possible????

0
bman9111
Asked:
bman9111
  • 4
  • 3
1 Solution
 
aaronfengCommented:
Yes, this is possible.  You can pretty much get a hold of anything in the DTE object model.  I also made VS Addin in the past, but I have switched to Visual Studio Integration Package because it is much easier to work with.  The down side of using the VS Integration Package is you need to apply for a key called PLK in order for you to distribute to others.  The PLK is free, but it's just a pain to apply for it (takes a few days to get it) .  If you are the only using it, you don't need the PLK, it will work on the machine you developed on.  In order to work with VS Integration Package you need to install Visual Studio SDK which can be found here: http://msdn.microsoft.com/vstudio/extend/

What you need to do is to parse Program.cs to get the startup form out.

Cheers,

Aaron
http://aaronfeng.blogspot.com
0
 
bman9111Author Commented:
could u give me an example of the steps i need to do what I listed above????

0
 
aaronfengCommented:
Sure, I will assume you know something about VS Addin already since you are trying to make one, so I'll get to the meat of it.  Basically you want to get the path of Program.cs from the opened solution in the DTE object model.  After you get it, you will open the file on disk and read it then popup the messagebox with the correct info.

// Here is how you get the path of Program.cs
string file = DTE.Solution.FindProjectItem("Program.cs").get_FileNames(0);

// Open and read the file using StreamReader
StreamReader reader = new StreamReader(file);

// Basicall you are looking for Application.Run(...);
// Maybe use RegEx to help out, but you probably just get it out by manipulate the string

// Show the user correct form in the message box
MessageBox.Show(formName);

If you need more help on creating VS Addin in generate, read this tutorial and source code, it will help a lot.
http://www.codeproject.com/useritems/LineCounterAddin.asp

Cheers,

Aaron
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bman9111Author Commented:
getting an error here...

  string file = DTE.Solution.FindProjectItem("Program.cs").get_FileNames(0);

Error      2      An object reference is required for the nonstatic field, method, or property 'EnvDTE._DTE.Solution.get'


what reference am i missing????
0
 
aaronfengCommented:
Basically you want to be able to get a DTE object, the code didn't work because DTE is a property that returns the object, so try (I'll assume you are doing a VS Addin not VS Integration):

// In the OnConnection method:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) {
  DTE2 dte = (DTE2)application;
  string file = dte.Solution.FindProjectItem("Program.cs").get_FileNames(0);
}

That should work, give that a try.

Cheers,

Aaron
0
 
bman9111Author Commented:
not sure where to put the above code....this is what i have....

i have the used the addin wizard in vs2005....then

then on the connect.cs....i have not touch anything except for add this to that class

      public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
            {
                  handled = false;
                  if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
                  {
                        if(commandName == "ListingComponents.Connect.ListingComponents")
                        {
                               Form1 frm = new Form1();
                     frm.Show();
                    handled = true;
                              return;
                   

                }
                  }
           
            }



then of course i add a form1 to the project...I have a command button on it and need to have a piece of code read the program.cs of a new project to see what the startup form is... like stated above....

seems like ur suggestion is for the connect.cs....really need something on the form level...

0
 
aaronfengCommented:
You want to have a member variable to hold the DTE2 object.

private DTE2 dte;

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) {
  dte = (DTE2)application;
}

   public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
          {
    // you can put this code anywhere, here is just an example.
    string file = dte.Solution.FindProjectItem("Program.cs").get_FileNames(0);
}

I'm just giving you the general outline, how this can be done.  Of course you want to follow general programming practices when you create this Addin.

Cheers,

Aaron
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now