0

I am getting the Object Reference Error (above) when implementing the following piece of code taken from the Microsoft Developers Netork (https://msdn.microsoft.com/en-us/library/ms404233(v=vs.100).aspx)

        SolutionBuild2 slnbld2 = (SolutionBuild2)DTE.Solution.SolutionBuild;

        //Save changes to all projects and clean.
        foreach (Project proj in DTE.Solution.Projects)
        {
            proj.Save();
        }
        slnbld2.Clean(true);

        foreach (Project proj in DTE.Solution.Projects)
        {

Am I missing something or should I easily be able to implement it as written? Any help is greatly appreciated!

EDIT


Public Module PublishAllProjects
    Sub PublishAllProjectsInSolution()
        ' Before using this macro, the certficate and security zone must be set.
        ' You can do this by publishing the projects using the VS IDE.
        Dim slnbld2 As SolutionBuild2 = CType(DTE.Solution.SolutionBuild, SolutionBuild2)

        'Save changes to all projects and clean.
        For Each proj As Project In DTE.Solution.Projects
            proj.Save()
        Next
        slnbld2.Clean(True)

        For Each proj As Project In DTE.Solution.Projects
            'Verify project is a windows application or console application before continuing
            Dim outputType As Integer = proj.Properties.Item("OutputType").Value
            If outputType <> 0 AndAlso outputType <> 1 Then
                Continue For
            End If

            'GenerateManifests and SignManifests must always to true for publishing to work. 
            proj.Properties.Item("GenerateManifests").Value = True
            proj.Properties.Item("SignManifests").Value = True
            proj.Save()

            slnbld2.BuildProject(proj.ConfigurationManager.ActiveConfiguration.ConfigurationName, proj.UniqueName, True)

            'only publish if build was successful.
            If slnbld2.LastBuildInfo <> 0 Then
                MsgBox("Build failed for " & proj.UniqueName)
            Else
                slnbld2.PublishProject(proj.ConfigurationManager.ActiveConfiguration.ConfigurationName, proj.UniqueName, True)
                If slnbld2.LastPublishInfo = 0 Then
                    MsgBox("Publish succeeded for " & proj.UniqueName)
                Else
                    MsgBox("Publish failed for " & proj.UniqueName)
                End If
            End If
        Next
    End Sub
End Module

The above code (sourced from https://msdn.microsoft.com/en-us/library/ms404233(v=vs.100).aspx) is VB but I have made an attempt to transcribe it to C#. I am hoping to be able to use it to be able to point to a solution on a file location and publish it.

When I transcribed it I got the error on every reference to DTE.Solution having imported the References for EnvDTE. I am hoping this is just a simple issue to overcome. I am relatively new to C# so can imagine (HOPE) it may well be! Chris

Tassisto
  • 9,877
  • 28
  • 100
  • 157
CJH
  • 1,266
  • 2
  • 27
  • 61
  • 1
    You are working from a VBA macro, no longer supported in VS. DTE is a special coclass, in VBA it magically creates a global variable with the same name thanks to the [appobject] attribute. The VBA runtime automatically creates the EnvDTE instance when it is first used. That same trick does not work in C#, you have to create the instance yourself. The context of this code is very unclear, doesn't look like it runs in an add-in or you wouldn't do it this way. Note the `dte` variable in [this snippet](http://stackoverflow.com/a/2852051/17034). – Hans Passant May 01 '16 at 08:48
  • Can you add some context to your question? Are you trying to write a Visual Studio Extension: http://stackoverflow.com/questions/3792333/vsix-getting-dte-object – Philip Pittle May 01 '16 at 11:21
  • I am ultimately trying to create an application that will publish a project from a given file path location. I have been finding it difficult to find anything on this specifically then come across the page (https://msdn.microsoft.com/en-us/library/ms404233(v=vs.100).aspx). I will update my original post with the full example given on that page but I was hoping to hijack it to my own ends. – CJH May 01 '16 at 15:23
  • In the context of my actual requirements (publish projects from File Locations) I think I may not even need this reference? I havent had the time to check that yet but im hoping I can create an instance of a Solution/Project for the solution saved on disk?? – CJH May 01 '16 at 15:33

0 Answers0