简体   繁体   English

使用Powershell将文件添加到项目

[英]Add files to project with powershell

I am hoping to leverage powershell in visual studio to add multiple files that have been dropped in a project directory to the project. 我希望利用Visual Studio中的Powershell将已删除在项目目录中的多个文件添加到项目中。 I have tried a few things to get this going, using hints found in this thread but am still running into a wall. 我尝试了一些方法来实现此目的,使用了在该线程中找到的提示,但仍遇到了麻烦。

This first attempt looks in the solution directory for any .cs files and tries to add them to the solution (not sure if that is really correct, as I want them added to the project, not the solution). 第一次尝试是在解决方案目录中查找任何.cs文件,然后尝试将它们添加到解决方案中(不确定是否确实正确,因为我希望将它们添加到项目中,而不是解决方案中)。

Split-Path -Parent -Path $dte.Solution.FullName | '
Get-ChildItem -Include @("*.cs") -Recurse | '
%{ [string]$_.FullName; $dte.Solution.AddFromFile([string]$_.FullName) }

This throws an error: Exception calling "AddFromFile" with "1" argument(s): "The template specified cannot be found. Please check that the full path is correct." 引发错误:异常调用带有“ 1”参数的“ AddFromFile”:“找不到指定的模板。请检查完整路径是否正确。” I am writing out the path to the file before I add it so I am able to verify that it is the correct path. 在添加文件之前,我正在写出该文件的路径,以便能够验证它是正确的路径。

I have also tried to use ExecuteCommand to add an existing item to the project thusly 我也尝试过使用ExecuteCommand将现有项目添加到项目中

Split-Path -Parent -Path $dte.Solution.FullName | '
Get-ChildItem -Include @("*.cs") -Recurse | '
%{ $dte.ExecuteCommand("Project.AddExistingItem", $_.FullName) }

The error here is: Exception calling "ExecuteCommand" with "2" argument(s): "Command "Project.AddExistingItem" does not accept arguments or switches." 这里的错误是:带有“ 2”参数的异常调用“ ExecuteCommand”:“命令“ Project.AddExistingItem”不接受参数或开关。”

Has anyone tried to do something similar and had success? 有没有人尝试做类似的事情并取得成功?

I have found a solution which I think solves the problem nicely. 我找到了可以很好解决问题的解决方案。 The command I am using is: 我正在使用的命令是:

 Split-Path -Parent -Path $dte.Solution.FullName | `
 Get-ChildItem -Include @("*.cs") -Recurse | `
 Select-Object FullName | `
 %{ $dte.Application.ItemOperations.AddExistingItem($_.FullName) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM