繁体   English   中英

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

namespace 'CopyItemCommand' could not be found, using Microsoft.PowerShell.Commands

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

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

The project with the error has a reference to 'Microsoft.PowerShell.Commands.Management' using the path " C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\v4.0_3.0.0.0__31bf3856ad364e35 \Microsoft.PowerShell.Commands.Management.dll ”,从我有限的理解来看,错误是因为项目引用了错误的版本或“Microsoft.PowerShell.Commands.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”后,我现在可以编译代码。

3 找不到命名空间Microsoft.Win32

我昨天安装了Visual Studio。 我想看看Visual Studio附带了什么版本的.NET Framework,所以我遵循了Microsoft的这段代码 。 (向下滚动代码)。 我使用“控制台应用程序(.NET Core)”打开了一个新的Visual C#项目,并在其中复制了给定 ...

9 无法找到命名空间 - 使用TFS构建[关闭]

我在使用TFS时遇到了麻烦。 它不会构建我的项目,因为 找不到类型或命名空间名称'Newtonsoft'(您是否缺少using指令或程序集引用?) 我尝试通过nuget添加它,但我仍然收到此错误。 当我通过nuget添加这个dll时,我有其他项目可以正常工作。 此外,该项 ...

10 找不到类型或名称空间

我有一个名为A16 Ex01的项目,并且其中包括了其他三个名为A16_Ex01_1 A16_Ex01_2 A16_Ex01_3项目,其中A16_Ex01_3是我们的入口点。 该项目A16_Ex01_3需要使用上所写的方法A16_Ex01_2 ,所以我已经包括了一个参考A16_Ex01_2在 ...

暂无
暂无

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

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