Visual Studio 2008 SDK - Create Project using SDK
Visual Studio 2008 SDK - Create Project using SDK
The following sample illustrates how to add new project into Visual Studio 2008 using the SDK, and add C# file into this project.
1) Create in Visual Studio 2008 new VsPackage project ('MyVSPackage') as shown in the following picture:
2) Choose C# as the language, and also select the Menu Item Command check box.
3) Add a reference to EnvDTE assembly.
EnvDTE is an assembly-based COM library, used for Visual Studio Automation. You can access projects, automate creation of solution and projects etc.
4) Put the following code in method: MyVSPackagePackage.MenuItemCallback()
private void MenuItemCallback(object sender, EventArgs e)
{
IVsSolution solution = (IVsSolution)GetService(typeof(SVsSolution));
if (solution != null)
{
IntPtr ppProject;
Guid rguidProjectType = Guid.Empty;
Guid iidProject = Guid.NewGuid();
uint grfCreateFlags = (uint)(
__VSCREATEPROJFLAGS.CPF_CLONEFILE |
__VSCREATEPROJFLAGS.CPF_OVERWRITE);
// Creates the project 'MyProject.csproj'.
Int32 result = solution.CreateProject(
ref rguidProjectType,
@"C:\Library.csproj",
@"C:\MyProj",
"MyProj",
grfCreateFlags,
ref iidProject,
out ppProject);
if (result != VSConstants.S_OK)
{
return;
}
IVsHierarchy ppHierarchy = null;
result = solution.GetProjectOfGuid(ref rguidProjectType, out ppHierarchy);
if (ppHierarchy != null && result == VSConstants.S_OK)
{
Object automationObject = null;
ppHierarchy.GetProperty(
VSConstants.VSITEMID_ROOT,
(Int32)__VSHPROPID.VSHPROPID_ExtObject,
out automationObject);
if (automationObject != null)
{
EnvDTE.SolutionClass sc = automationObject as EnvDTE.SolutionClass;
EnvDTE.Projects projects = sc.Projects;
EnvDTE.Project project = projects.Item(1);
EnvDTE.ProjectItems pitems = project.ProjectItems;
pitems.AddFromFileCopy(@"c:\MyCS.cs");
}
}
}
}
-
Interface IVsSolution - Provides a top level maintanance for the solution. This interface is part of the Microsoft.VisualStudio.Shell.Interop Namespace.
-
Method IVsSolution.CreateProject() - Creates the new project (can also open a project).
5) Run the solution, and an Experimental Visual Studio will open.
Select Tools-->My Command Name to trigger the MenuItemCallback(), a new project 'MyProj' is added including a C# file (MyCS.cs).
The source code for this project can be downloaded here.