使用 Microsoft.PowerShell.Commands 找不到命名空间“CopyItemCommand”

Posted

技术标签:

【中文标题】使用 Microsoft.PowerShell.Commands 找不到命名空间“CopyItemCommand”【英文标题】:namespace 'CopyItemCommand' could not be found, using Microsoft.PowerShell.Commands 【发布时间】:2022-01-11 16:59:09 【问题描述】:

根据我的任务,我一直在构建一个名为 CoApp 的旧库,我使用的是 Visual Studio 2013,我不得不对 NuGet 版本和引用路径进行一些小的更改,例如“System.Management.Automation”和到目前为止,我在成功构建 20 个解决方案中的 17 个方面取得了很大进展,我只剩下一个错误“找不到命名空间‘CopyItemCommand’”,这阻止了最后 3 个项目的构建,我正在努力寻找原因。我认为 Visual Studio 正在查找引用,因为“CopyItemCommand”的下划线是绿色而不是红色,当我将鼠标悬停在它上面时它会显示正确的命名空间?

出现错误的项目使用路径“C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\v4 引用了“Microsoft.PowerShell.Commands.Management” .0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Commands.Management.dll”,从我有限的理解来看,这个错误是因为项目引用了错误的版本或“Microsoft.PowerShell.Commands.Management”,就像'System.Management.Automation' 的案例,但是我没有找到任何有用的信息来说明要使用的正确版本以及在哪里可以找到它。

请问有谁知道我可以尝试解决这个错误吗?

//-----------------------------------------------------------------------
// <copyright company="CoApp Project">
//     Copyright (c) 2010-2013 Garrett Serack and CoApp Contributors. 
//     Contributors can be discovered using the 'git log' command.
//     All rights reserved.
// </copyright>
// <license>
//     The software is licensed under the Apache 2.0 License (the "License")
//     You may not use the software except in compliance with the License. 
// </license>
//-----------------------------------------------------------------------

namespace ClrPlus.Powershell.Provider.Commands 
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Management.Automation;
    using System.Threading;
    using System.Threading.Tasks;
    using Base;
    using Core.Exceptions;
    using Core.Extensions;
    using Filesystem;
    using Utility;
    using Microsoft.PowerShell.Commands;


    [Cmdlet("Copy", "ItemEx", DefaultParameterSetName = "Selector", SupportsShouldProcess = true, SupportsTransactions = false)]
    public class CopyItemExCmdlet : CopyItemCommand 
        public static ILocationResolver GetLocationResolver(ProviderInfo providerInfo) 
            var result = providerInfo as ILocationResolver;
            if (result == null) 
                if (providerInfo.Name == "FileSystem") 
                    return new FilesystemLocationProvider(providerInfo);
                
            
            if (result == null) 
                throw new ClrPlusException("Unable to create location resolver for 0".format(providerInfo.Name));
            
            return result;
        

        private readonly CancellationTokenSource _cancellationToken = new CancellationTokenSource();
    

        protected override void BeginProcessing() 
           // Console.WriteLine("===BeginProcessing()===");
            base.BeginProcessing();
        

        protected override void EndProcessing() 
            //Console.WriteLine("===EndProcessing()===");
            base.EndProcessing();
        

        protected virtual void Process(ProviderInfo sourceProvider, IEnumerable<string> sourcePaths, ProviderInfo destinationProvider, string destinationPath) 
        

        protected override void ProcessRecord() 
            ProviderInfo destinationProviderInfo;
   

      

            var destinationLocation = ResolveDestinationLocation(out destinationProviderInfo);

       


            var sources = Path.Select(each => 
                ProviderInfo spi;
                var sourceFiles = SessionState.Path.GetResolvedProviderPathFromPSPath(each, out spi);
                return new SourceSet 
                    ProviderInfo = spi,
                    SourcePaths = sourceFiles.ToArray(),
                ;
            ).ToArray();


       

       

            var providerInfos = sources.Select(each => each.ProviderInfo).Distinct().ToArray();
            if (providerInfos.Length == 1 && providerInfos[0] == destinationProviderInfo) 
                WriteVerbose("Using regular copy-item");
                base.ProcessRecord();
                return;
            

            bool force = Force;
        
       
        

       



            var copyOperations = ResolveSourceLocations(sources, destinationLocation).ToArray();

            if (copyOperations.Length > 1 && destinationLocation.IsFile) 
                // source can only be a single file.
                ThrowTerminatingError(new ErrorRecord(new DirectoryNotFoundException(), "0", ErrorCategory.InvalidArgument, null));
                //WriteError(new ErrorRecord(new ClrPlusException("Destination file exists--multiple source files specified."), "ErrorId", ErrorCategory.InvalidArgument, null));
                return;
            


            var s = new Stopwatch();
            s.Start();
            for  (var i = 0; i < copyOperations.Length;  i++) 
                var operation = copyOperations[i];
                WriteProgress(CreateProgressRecord(1, "Copy", "Copying item 0 of 1".format(i, copyOperations.Length), 100 * (double)i/copyOperations.Length));

                //Console.WriteLine("COPY '0' to '1'", operation.Source.AbsolutePath, operation.Destination.AbsolutePath);
                if (!force) 
                    if (operation.Destination.Exists) 
                        ThrowTerminatingError(new ErrorRecord(new ClrPlusException("Destination file '0' exists. Must use -force to override".format(operation.Destination.AbsolutePath)), "ErrorId", ErrorCategory.ResourceExists, null));
                        return;
                    
                


            
                using (var inputStream = new ProgressStream(operation.Source.Open(FileMode.Open))) 
                    using (var outputStream = new ProgressStream(operation.Destination.Open(FileMode.Create))) 

                        var inputLength = inputStream.Length;
                  
                        inputStream.BytesRead += (sender, args) => ;
                        CopyOperation operation1 = operation;
                        outputStream.BytesWritten += (sender, args) => WriteProgress(CreateProgressRecord(2, "Copy",
                            "Copying '0' to '1'".format(operation1.Source.AbsolutePath, operation1.Destination.AbsolutePath), 100*(double)args.StreamPosition/inputLength, 1));
                     
                        inputStream.CopyTo(outputStream, 32768);

                        /*
                        Task t = inputStream.CopyToAsync(outputStream, _cancellationToken.Token, false);
                        try 
                            t.RunSynchronously();
                         catch (TaskCanceledException e) 
                            return;
                        
                        */
                    
                

                WriteProgress(CreateCompletedProgressRecord(2, "Copy",
                            "Copying '0' to '1'".format(operation.Source.AbsolutePath, operation.Destination.AbsolutePath), 1));

               // WriteVerbose("Copy from 0 to 1".format(operation.Source.AbsolutePath, operation.Destination.AbsolutePath));
            
            WriteProgress(CreateCompletedProgressRecord(1, "Copy", "Copy finished"));
            s.Stop();
            WriteVerbose("Completed in 0".format(s.Elapsed));
       
        


   

        private ILocation ResolveDestinationLocation(out ProviderInfo destinationProviderInfo) 
        
            try 
                //if Destination doesn't exist, this will throw
                var destination = SessionState.Path.GetResolvedProviderPathFromPSPath(Destination, out destinationProviderInfo);
                var path = destination[0];
            
                return GetLocationResolver(destinationProviderInfo).GetLocation(path);
            
             catch (Exception) 
                //the destination didn't exist, probably a file
                var lastSlash = Destination.LastIndexOf('\\');
                var hasASlash = lastSlash >= 0;
                var probablyDirectoryDestination = hasASlash ? Destination.Substring(0, lastSlash) : ".";
                //if this throws not even the directory exists
                var destination = SessionState.Path.GetResolvedProviderPathFromPSPath(probablyDirectoryDestination, out destinationProviderInfo);

                var path = destination[0];
                path += hasASlash ? Destination.Substring(lastSlash) : @"\" + Destination;
            
                return GetLocationResolver(destinationProviderInfo).GetLocation(path);
            
        

   
    


    

        private ProgressRecord CreateProgressRecord(int activityId, string activity, string statusDescription, double percentComplete, int parentActivityId = 0) 
            return new ProgressRecord(activityId, activity, statusDescription) 
                                                                                      PercentComplete = (int)percentComplete,
                                                                                      ParentActivityId = parentActivityId
                                                                                  ;

        

        private ProgressRecord CreateCompletedProgressRecord(int activityId, string activity, string statusDescription, int parentActivityId = 0) 
            return new ProgressRecord(activityId, activity, statusDescription) 
                                                              RecordType = ProgressRecordType.Completed,
                                                              ParentActivityId = parentActivityId
                                                          ;
        

        internal virtual IEnumerable<CopyOperation> ResolveSourceLocations(SourceSet[] sourceSet, ILocation destinationLocation) 
            bool copyContainer = this.Container;

            foreach (var src in sourceSet) 
                var resolver = GetLocationResolver(src.ProviderInfo);
                foreach (var path in src.SourcePaths) 
                    var location = resolver.GetLocation(path);
                    var absolutePath = location.AbsolutePath;

                    if (!location.IsFile) 
                        // if this is not a file, then it should be a container.
                        if (!location.IsItemContainer) 
                            throw new ClrPlusException("Unable to resolve path '0' to a file or folder.".format(path));
                        

                        // if it's a container, get all the files in the container
                        var files = location.GetFiles(Recurse);
                        foreach (var f in files) 
                            var relativePath = (copyContainer ? location.Name + @"\\" : "") + absolutePath.GetRelativePath(f.AbsolutePath);
                            yield return new CopyOperation 
                                Destination = destinationLocation.IsFileContainer ? destinationLocation.GetChildLocation(relativePath) : destinationLocation,
                                Source = f
                            ;
                        
                        continue;
                    

                    yield return new CopyOperation 
                        Destination = destinationLocation.IsFileContainer ?  destinationLocation.GetChildLocation(location.Name) : destinationLocation,
                        Source = location
                    ;
                
            
        

        protected override void StopProcessing() 
      
            base.StopProcessing();
            _cancellationToken.Cancel();
        
        
    

【问题讨论】:

请将您的代码发布为文本而不是屏幕截图。请包含项目文件中的包引用 【参考方案1】:

我已修复错误,Microsoft.PowerShell.Commands.dll 依赖于相同版本的 System.Management.Automation.dll。将 Microsoft.PowerShell.Commands.dll 更改为相同版本“1.0.0.0__31bf3856ad364e35”后,我现在可以编译代码了。

【讨论】:

以上是关于使用 Microsoft.PowerShell.Commands 找不到命名空间“CopyItemCommand”的主要内容,如果未能解决你的问题,请参考以下文章

在使用加载数据流步骤的猪中,使用(使用 PigStorage)和不使用它有啥区别?

今目标使用教程 今目标任务使用篇

Qt静态编译时使用OpenSSL有三种方式(不使用,动态使用,静态使用,默认是动态使用)

MySQL db 在按日期排序时使用“使用位置;使用临时;使用文件排序”

使用“使用严格”作为“使用强”的备份

Kettle java脚本组件的使用说明(简单使用升级使用)