Monday, October 25, 2010

SharpDevelop:- ProjectItems

This blog entry will be all about adding the projectitem to the SharpDevelop project. In SharpDevelop we get project service from the class "ProjectService" that lets us to add various type of project item to the SharpDevelop project and bind to the the different project related events. ProjectService class can be found in main base addin ("\Project\Src\Services\ProjectService\ProjectService.cs"). SharpDevelop project item can be of different types like cs/vb files , project references , webreferences etc. Main base class for all the project items in SharpDevelop is "ProjectItem" Class from which all the derived project item classes evolve like FileProjectItem, ReferenceProjectItem, WebReferenceProjectItem etc.
In this blog we will see the addition of assembly references and the addition of cs/vb class files to the SharpDevelop Project at runtime.

Once the project is opened in the SharpDevelop we can access the current project via ProjectService class ( "ProjectService.CurrentProject" property ).

To add the assembly reference at runtime we have to create the instance of ReferenceProjectItem and provide the current project instance and the assembly name in the constructor.

ReferenceProjectItem item = new ReferenceProjectItem(ProjectService.CurrentProject, Path.GetFileNameWithoutExtension(my_exsistingCSFilePath));

Prior to adding the assembly reference to the Project , we can check if assembly reference is already added to the Project. We can check that by checking all the current project items .

IProject project = ProjectService.CurrentProject;


ProjectItem proitem = (from proI in project.Items.AsEnumerable() where Path.GetFileNameWithoutExtension(proI.FileName) == Path.GetFileNameWithoutExtension (my_exsistingCSFilePath) select proI).SingleOrDefault();


if (proitem != null){ ProjectService.RemoveProjectItem(project, proitem);
}


After checking for the exsistence of the assembly reference we can add the assembly reference to the Project at runtime by:-


ProjectService.AddProjectItem(project, item);


project.Save();


ProjectBrowserPad.RefreshViewAsync();


To add the cs/vb project item to Project at runtime , we have to create the instance of "FileProjectItem" and provide appropriate parameters to the constructor.

IProject project = ProjectService.CurrentProject;


string relFileName = FileUtility.GetRelativePath(project.Directory, filePath);


FileProjectItem fileProjectItem = new FileProjectItem(project, project.GetDefaultItemType(filePath), relFileName);


Then we can add the file to the current project , following the same procedure as we did in addition of assembly reference.

ProjectItem item = (from proI in project.Items.AsEnumerable() where Path.GetFileName(proI.FileName) == Path.GetFileName(filePath) select proI).SingleOrDefault();


if(item != null) {ProjectService.RemoveProjectItem(project, item); }


FileNode fileNode = new FileNode(filePath, FileNodeStatus.InProject);

fileNode.ProjectItem = fileProjectItem; ProjectService.AddProjectItem(project, fileProjectItem);

project.Save();

ProjectBrowserPad.RefreshViewAsync();


In this same way we can add WebReferences and all other project items to the SharpDevelop Project.

No comments:

Post a Comment