Report error/warning if missing files in project/solution in Visual Studio

后端 未结 4 1667
谎友^
谎友^ 2020-12-31 11:45

Is there a way for Visual Studio to report an error/warning when you build a solution that has missing files (yellow triangle icon with exclamation) that do have necessarily

4条回答
  •  心在旅途
    2020-12-31 11:56

    You need to define an EnvironmentEvents macro. See the general description on how to do this here: Customize Your Project Build Process.

    And here is the code you can directly paste in the macro environment to check missing files:

    Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
        For Each proj As Project In DTE.Solution.Projects
            For Each item As ProjectItem In proj.ProjectItems
                If (item.Kind <> "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}") Then ' only check physical file items
                    Continue For
                End If
    
                For i As Integer = 1 To item.FileCount
                    Dim path As String = item.FileNames(i)
                    If Not System.IO.File.Exists(item.FileNames(i)) Then
                        WriteToBuildWindow("!! Missing file:" & item.FileNames(i) + " in project " + proj.Name)
                    End If
                Next
            Next
        Next
    End Sub
    
    Public Sub WriteToBuildWindow(ByVal text As String)
        Dim ow As OutputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object
        Dim build As OutputWindowPane = ow.OutputWindowPanes.Item("Build")
        build.OutputString(text & Environment.NewLine)
    End Sub
    

    It will display the "missing file" text directly in the Visual Studio "Build" output window. It should be fairly easy to understand and tune to your needs. For example, you could add errors to the error output.

提交回复
热议问题