这是使用 PowerShell 的解决方案。它有点长,但我保证它有效。我测试了很多。
首先,简单的部分。以下是从命令提示符运行脚本的方式。
powershell -File C:\AddExistingItem.ps1 -solutionPath "C:\Test.sln" -projectName "TestAddItem" -item "C:\Test.txt"
现在是可怕的部分,AddExistingItem.ps1:
param([String]$solutionPath, [String]$projectName, [String]$item)
#BEGIN: section can be removed if executing from within a PowerShell window
$source = @"
namespace EnvDteUtils
{
using System;
using System.Runtime.InteropServices;
public class MessageFilter : IOleMessageFilter
{
//
// Class containing the IOleMessageFilter
// thread error-handling functions.
// Start the filter.
public static void Register()
{
IOleMessageFilter newFilter = new MessageFilter();
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(newFilter, out oldFilter);
}
// Done with the filter, close it.
public static void Revoke()
{
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(null, out oldFilter);
}
//
// IOleMessageFilter functions.
// Handle incoming thread requests.
int IOleMessageFilter.HandleInComingCall(int dwCallType,
System.IntPtr hTaskCaller, int dwTickCount, System.IntPtr
lpInterfaceInfo)
{
//Return the flag SERVERCALL_ISHANDLED.
return 0;
}
// Thread call was rejected, so try again.
int IOleMessageFilter.RetryRejectedCall(System.IntPtr
hTaskCallee, int dwTickCount, int dwRejectType)
{
if (dwRejectType == 2)
// flag = SERVERCALL_RETRYLATER.
{
// Retry the thread call immediately if return >=0 &
// <100.
return 99;
}
// Too busy; cancel call.
return -1;
}
int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee,
int dwTickCount, int dwPendingType)
{
//Return the flag PENDINGMSG_WAITDEFPROCESS.
return 2;
}
// Implement the IOleMessageFilter interface.
[DllImport("Ole32.dll")]
private static extern int
CoRegisterMessageFilter(IOleMessageFilter newFilter, out
IOleMessageFilter oldFilter);
}
[ComImport(), Guid("00000016-0000-0000-C000-000000000046"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(
int dwCallType,
IntPtr hTaskCaller,
int dwTickCount,
IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(
IntPtr hTaskCallee,
int dwTickCount,
int dwRejectType);
[PreserveSig]
int MessagePending(
IntPtr hTaskCallee,
int dwTickCount,
int dwPendingType);
}
}
"@
Add-Type -TypeDefinition $source
[EnvDTEUtils.MessageFilter]::Register()
#END: section can be removed if executing from within a PowerShell window
$IDE = New-Object -ComObject VisualStudio.DTE
$IDE.Solution.Open("$solutionPath")
$project = $IDE.Solution.Projects | ? { $_.Name -eq "$projectName" }
$project.ProjectItems.AddFromFile("$item") | Out-Null
$project.Save()
$IDE.Quit()
#BEGIN: section can be removed if executing from within a PowerShell window
[EnvDTEUtils.MessageFilter]::Revoke()
#END: section can be removed if executing from within a PowerShell window
95% 的代码只是为了让您从命令提示符运行。如果您是直接在 PowerShell 中编写和运行代码,您可以将其排除在外,直接转到$IDE = New-Object -ComObject VisualStudio.DTE
.
这是一篇博客文章,解释了为什么需要这些可怕的东西。
这是另一个关于同一件事的,但在 C# 中。
还有一点值得注意。我尝试使用 EnvDTE 程序集,就像在 .net 中一样,但我一直收到 COM 注册错误。一旦我开始直接使用 COM 对象,一切正常。我对 COM 的了解还不够,无法真正冒险猜测这是为什么。
编辑
如果您在尝试从命令提示符运行脚本时收到此错误:
然后你需要先运行它(你应该只需要运行一次。)
powershell -command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned"
这是对该命令的作用的一个很好的、深入的解释。