public virtual void Init(string fileName, string destination, string projectName, __VSCREATEPROJFLAGS createFlags)
{
if (String.IsNullOrEmpty(fileName))
{
throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "fileName");
}
if (String.IsNullOrEmpty(destination))
{
throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "destination");
}
this._projectName = Path.GetFileName(fileName);
this.ProjectPath = Path.Combine(destination, this._projectName);
// get the IVsSolution interface from the global service provider
IVsSolution solution = this.GetService(typeof(IVsSolution)) as IVsSolution;
Debug.Assert(solution != null, "Could not get the IVsSolution object from the services exposed by this project");
if (solution == null)
{
throw new InvalidOperationException();
}
// Get the project type guid from project element
string typeGuidString = this.ItemNode.GetMetadataAndThrow(ProjectFileConstants.TypeGuid, new InvalidOperationException());
Guid projectFactoryGuid = Guid.Empty;
if (!String.IsNullOrEmpty(typeGuidString))
{
projectFactoryGuid = new Guid(typeGuidString);
}
// Get the project factory.
IVsProjectFactory projectFactory;
ErrorHandler.ThrowOnFailure(solution.GetProjectFactory((uint)0, new Guid[] { projectFactoryGuid }, fileName, out projectFactory));
this.CreateProjectDirectory();
//Create new project using factory
int cancelled;
Guid refiid = VSConstants.IID_IUnknown;
IntPtr projectPtr = IntPtr.Zero;
try
{
ErrorHandler.ThrowOnFailure(projectFactory.CreateProject(fileName, destination, projectName, (uint)createFlags, ref refiid, out projectPtr, out cancelled));
if (projectPtr != IntPtr.Zero)
{
this.nestedHierarchy = Marshal.GetTypedObjectForIUnknown(projectPtr, typeof(IVsHierarchy)) as IVsHierarchy;
Debug.Assert(this.nestedHierarchy != null, "Nested hierarchy could not be created");
Debug.Assert(cancelled == 0);
}
}
finally
{
if (projectPtr != IntPtr.Zero)
{
// We created a new instance of the project, we need to call release to decrement the ref count
// the RCW (this.nestedHierarchy) still has a reference to it which will keep it alive
Marshal.Release(projectPtr);
}
}
if (cancelled != 0 && this.nestedHierarchy == null)
{
ErrorHandler.ThrowOnFailure(VSConstants.OLE_E_PROMPTSAVECANCELLED);
}
// Link into the nested VS hierarchy.
ErrorHandler.ThrowOnFailure(this.nestedHierarchy.SetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ParentHierarchy, this.ProjectManager));
ErrorHandler.ThrowOnFailure(this.nestedHierarchy.SetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ParentHierarchyItemid, (object)(int)this.Id));
this.LockRdtEntry();
this.ConnectPropertyNotifySink();
}