gpt4 book ai didi

asp.net - 从命令行将文件包含在项目中

转载 作者:行者123 更新时间:2023-12-01 18:34:15 28 4
gpt4 key购买 nike

有没有办法在 vs2012 中从命令行将文件包含在项目中?

我之所以问这个问题,是因为每当我使用其他 IDE(如 ST3)或从 Photoshop 等保存文件时,将添加到项目文件夹中的任何新文件包含在内是非常令人沮丧的。

我正在使用 Grunt 进行大量缩小、串联、在我的角度脚本上运行 ngmin 等。有一个 grunt-shell 插件允许 grunt 任务运行 shell 命令(我已经使用它来解锁锁定)由 TFS 文件)。所以我想我可以创建一个任务,为我添加的任何新文件包含在项目中(通过使用 grunt-watch 监视某个文件夹)。

最佳答案

这是使用 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

Here是一篇博客文章,解释了为什么需要那些可怕的东西。
here是另一篇关于同一件事的文章,但是是用 C# 编写的。

还有一件事值得注意。我尝试使用 EnvDTE 程序集,就像在 .net 中一样,但我不断收到 COM 注册错误。一旦我开始直接使用 COM 对象,一切就都正常了。我对 COM 的了解还不够,无法真正猜测这是为什么。

编辑

如果您在尝试从命令提示符运行脚本时收到此错误:

Execution Policy Error

然后您需要先运行它(您应该只需要运行一次。)

powershell -命令“Set-ExecutionPolicy -ExecutionPolicy RemoteSigned”

Here很好、深入地解释了该命令的作用。

关于asp.net - 从命令行将文件包含在项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17382308/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com