private void btnStartVS_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
EnvDTE80.DTE2 dte;
dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject(“VisualStudio.DTE.11.0”);
sb.AppendLine(dte.FullName);
Int32 i = dte.Solution.Count;
UIHierarchy UIH = dte.ToolWindows.SolutionExplorer;
UIHierarchyItems items = UIH.UIHierarchyItems;
Int32 indent = 0;
foreach (UIHierarchyItem uih in dte.ToolWindows.SolutionExplorer.UIHierarchyItems)
{
displayItems(uih.UIHierarchyItems, sb, indent);
}
dte.ToolWindows.OutputWindow.ActivePane.OutputString(sb.ToString());
try {
Solution2 soln = (Solution2)dte.Solution;
// show us the solution name
string SolnName = System.IO.Path.GetFileNameWithoutExtension(soln.FullName);
/**
MessageBox.Show("Adding a folder to the solution " + solnName);
Project proj = soln.AddSolutionFolder("PL999");
MessageBox.Show("Solution : " + solnName + " has " + soln.Projects.Count.ToString() + " projects");
**/
Console.WriteLine("Solution : {0} has now {1} projects", SolnName, soln.Projects.Count.ToString());
String ProjName = "IM00100851";
String ProjPath = @"E:\Users\C770817\SW-PROJEKTE\Spezpla\"+ProjName;
String ProjFullName = ProjPath + @"\" + ProjName + ".oxygene";
Project proj = null;
// lets find the project because if it is lready in soln ....
Projects ps = soln.Projects;
foreach (Project p in ps)
{
if (ProjName.CompareTo(p.Name) == 0)
{
proj = p;
}
}
if (proj == null)
{
// if proj == null add it the one or the other way
if (Directory.Exists(ProjPath) && (File.Exists(ProjFullName)))
{
// if path exits try to add from project file
Console.WriteLine("Project {0} seems to exist already, try adding it to solution {1}",
ProjFullName, SolnName);
proj = soln.AddFromFile(ProjFullName);
Console.WriteLine("Project {0} added to solution {1}",
ProjFullName, SolnName);
}
else
{
// if path does not exists make a new directory and add a project from a vstemplate
Console.WriteLine("Creating Directory {0}", ProjPath);
DirectoryInfo di = Directory.CreateDirectory(ProjPath);
Console.WriteLine("Directory {0} created!", ProjPath);
String TemplatePath = soln.GetProjectTemplate("Class Library", "Oxygene");
Console.WriteLine("Project {0} will be added to solution {1} from Template\n {2}",
ProjName, SolnName, TemplatePath);
proj = soln.AddFromTemplate(TemplatePath, ProjPath, ProjName, false);
Console.WriteLine("Project {0} added to solution {1}", ProjName, SolnName);
}
}
Console.WriteLine("Solution : {0} has now {1} projects", SolnName, soln.Projects.Count.ToString());
// lets find the project because AddFrom... does not return it
ps = soln.Projects;
foreach (Project p in ps)
{
if (ProjName.CompareTo(p.Name) == 0)
{
proj = p;
}
}
if (proj==null)
{
// if it remains null it was not found or not created in solution
Console.WriteLine("Project {0} not found in Solution {1}", ProjName, SolnName);
return;
}
// now lets attempt to add an existing code file to the project
String vmsSourceFile = @"E:\Users\C770817\SW-PROJEKTE\Spezpla\Pas2Oxygene\PL999.PAS";
ProjectItem pi = proj.ProjectItems.AddFromFileCopy(vmsSourceFile);
foreach (Property p in pi.Properties)
{
if ("BuildAction".CompareTo(p.Name)==0)
{
p.Value = 2;
}
if ("ExcludeFromBuild".CompareTo(p.Name) == 0)
{
p.Value = true;
}
Console.WriteLine("Property.Name: {0} .Value: {1}", p.Name, p.Value);
}
// if it was found, for now, ask user to remove it from solution
DialogResult yesno = MessageBox.Show("Shall we Remove the project from the Solution?","QUESTION",
MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
if ( yesno==DialogResult.Yes )
{
Console.WriteLine("Project {0} will be removed from solution {1}", proj.Name, SolnName);
soln.Remove(proj);
Console.WriteLine("Project {0} removed from solution {1}", ProjName, SolnName);
}
Console.WriteLine("Solution : {0} has now {1} projects", SolnName, soln.Projects.Count.ToString());
}
catch (SystemException ex)
{
Console.WriteLine("ERROR: {0}\n {1}", ex.Message, ex.StackTrace.ToString());
}
}
private void displayItems(UIHierarchyItems uih, StringBuilder sb, Int32 indent)
{
indent = indent + 2;
foreach (UIHierarchyItem id in uih)
{
String ins = new String(' ', indent);
sb.AppendLine(ins+id.Name);
if (id.Name.CompareTo("Form1.cs") == 0)
{
MessageBox.Show(id.Name);
}
displayItems(id.UIHierarchyItems, sb, 2);
}
indent = indent - 2;
}
No this is the code (included above) used so far to change two properties;
foreach (Property p in pi.Properties)
{
if ("BuildAction".CompareTo(p.Name)==0)
{
p.Value = 2;
}
if ("ExcludeFromBuild".CompareTo(p.Name) == 0)
{
p.Value = true;
}
Console.WriteLine("Property.Name: {0} .Value: {1}", p.Name, p.Value);
}
What I dont know is why I can not see any of the projects properties named as seen in the oxygene project file (xml file)
what I see from a code run such as above is:
Property.Name: IsLink .Value: False
'WCFSpezplaServiceClient.vshost.exe' (Managed (v4.0.30319)): Loaded 'Anonymously Hosted DynamicMethods Assembly'
Property.Name: SubType .Value: Code
Property.Name: CopyToOutputDirectory .Value: 0
Property.Name: DefaultNamespace .Value: IM00100851
Property.Name: CopyDestination .Value: Folder
Property.Name: DisableDesigner .Value: False
Property.Name: ExcludeFromBuild .Value: True
Property.Name: FullyQualifiedResourceName .Value: True
Property.Name: CustomTool .Value:
Property.Name: CustomToolNamespace .Value:
Property.Name: BuildAction .Value: 2
Property.Name: DateModified .Value: 28.08.2014 11:47
Property.Name: IsDependentFile .Value: False
Property.Name: ItemType .Value: Content
Property.Name: URL .Value: file:///E:\Users\C770817\SW-PROJEKTE\Spezpla\IM00100851\PL999.PAS
Property.Name: __id .Value: PL999.PAS
Property.Name: BuildActionInternal .Value: 2
Property.Name: FileName .Value: PL999.PAS
Property.Name: FullPath .Value: E:\Users\C770817\SW-PROJEKTE\Spezpla\IM00100851\PL999.PAS
Property.Name: Extension .Value: .PAS
Property.Name: ExtenderCATID .Value: {eb066e96-c58d-4b48-9a37-df90becf2d9b}
Property.Name: ExtenderNames .Value: System.String[]
Property.Name: ServiceProvider .Value: System.__ComObject
Solution : Spezpla has now 27 projects