当您构建的解决方案缺少文件(带有感叹号的黄色三角形图标),而这些文件确实会导致编译错误时,Visual Studio是否有办法报告错误/警告?例如在运行时读取的丢失的配置文件。
谢谢
发布于 2011-09-10 14:44:35
您需要定义一个EnvironmentEvents macro。有关如何执行此操作的一般描述,请参阅此处:Customize Your Project Build Process。
下面是您可以直接粘贴到宏环境中的代码,用于检查缺少的文件:
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它将直接在Visual Studio的"Build“输出窗口中显示"missing file”文本。它应该相当容易理解和调整到您的需求。例如,可以将错误添加到错误输出中。
发布于 2012-10-12 02:19:30
当我们有丢失的文件时,我们会得到疯狂的编译错误,比如即使文件最终被写入,也无法编写xyz.pdb。我接受了Simon提供的东西(谢谢!)我稍微改变了一下;具体地说,我添加了一些递归,并添加了对有子文件的文件夹和文件(例如数据集、代码后面的文件)的支持。
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
walkTree(proj.ProjectItems, False)
Next
End Sub
Private Sub walkTree(ByVal list As ProjectItems, ByVal showAll As Boolean)
For Each item As ProjectItem In list
' from http://msdn.microsoft.com/en-us/library/z4bcch80(v=vs.80).aspx
' physical files: {6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}
' physical folders: {6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}
If (item.Kind = "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}" OrElse _
item.Kind = "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}") Then
For i As Integer = 1 To item.FileCount ' appears to be 1 all the time...
Dim existsOrIsFolder As Boolean = (item.Kind = "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}" OrElse System.IO.File.Exists(item.FileNames(i)))
If (showAll OrElse _
existsOrIsFolder = False) Then
WriteToBuildWindow(String.Format("{0}, {1}, {2} ", existsOrIsFolder, item.ContainingProject.Name, item.FileNames(i)))
End If
Next
If (item.ProjectItems.Count > 0) Then
walkTree(item.ProjectItems, showAll)
End If
End If
Next
End Sub
Private 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发布于 2015-11-21 00:46:33
如果其他人偶然发现了这个线程,当你的项目中缺少文件时,有一个plugin会给你一个构建错误。
https://stackoverflow.com/questions/3145469
复制相似问题