加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 从命令行包含项目中的文件

发布时间:2020-12-16 04:34:57 所属栏目:asp.Net 来源:网络整理
导读:有没有办法在vs2012的命令行中包含项目中的文件? 我问的原因是因为每当我使用其他IDE(如ST3)或从Photoshop保存文件时,包含我添加到项目文件夹中的任何新文件都非常令人沮丧. 我正在使用Grunt进行大量的缩小,连接,在我的角度脚本上运行ngmin等.有一个grunt-s
有没有办法在vs2012的命令行中包含项目中的文件?

我问的原因是因为每当我使用其他IDE(如ST3)或从Photoshop保存文件时,包含我添加到项目文件夹中的任何新文件都非常令人沮丧.

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

解决方法

这是使用Power Shell的解决方案.它有点长,但我保证它有效.我测试了很多.

首先,简单的部分.以下是从命令提示符运行脚本的方法.

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 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 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,IntPtr lpInterfaceInfo); 

        [PreserveSig] 
        int RetryRejectedCall(  
            IntPtr hTaskCallee,int dwRejectType); 

        [PreserveSig] 
        int MessagePending(  
            IntPtr hTaskCallee,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真的冒险猜测为什么会这样.

编辑

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

然后你需要先运行它(你应该只需要运行一次.)

powershell -command“Set-ExecutionPolicy -ExecutionPolicy RemoteSigned”

Here是对该命令正在做什么的一个很好的,深入的解释.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读