开发者

Problem with Visual Studio macro to remove a project from a solution

I have recorded a macro with me selecting a project and removing it from the solution, resulting in this cod开发者_开发知识库e:

DTE.ActiveWindow.Object.GetItem("PerfixEMS\Allocations\BDAUploader\perFIXAllocationsFTP").Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ExecuteCommand("Edit.Delete")

This macro works nicely if the project is in the solution, but causes a runtime error if it has already been removed. Even assigning the result if GetItem to an Object variable, to check for Nothing before calling Select, causes a runtime error. How can I error proof this macro?


You can put that in a Try block:

Try
    DTE.ActiveWindow.Object.GetItem("PerfixEMS\Allocations\BDAUploader\perFIXAllocationsFTP").Select(vsUISelectionType.vsUISelectionTypeSelect)
    DTE.ExecuteCommand("Edit.Delete")
Catch
End Try

Update: I think your macro is not very safe. It uses active window and that window could be any window. I suggest something like this:

Try
    DTE.Solution.Remove(DTE.Solution.Projects.Item("test.vcproj"))
Catch
End Try

Project is referenced by its UniqueName property here. That name is projects path relative to the solution and its file extension. You can explore the properties by running a macro under the macro debugger. Projects can also be referenced by their 1-based index (DTE.Solution.Projects.Item(1)).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜