gpt4 book ai didi

c# - 使用 Microsoft.PowerShell.Commands 找不到命名空间 'CopyItemCommand'

转载 作者:行者123 更新时间:2023-12-04 17:05:13 25 4
gpt4 key购买 nike

根据我的任务,我一直在构建一个名为 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.0_3.0.0.0__31bf3856ad364e35”引用了“Microsoft.PowerShell.Commands.Management”\Microsoft.PowerShell.Commands.Management.dll”,根据我有限的理解,错误是因为项目引用了错误的版本或“Microsoft.PowerShell.Commands.Management”,就像“System.Management.Automation”一样我没有找到任何关于要使用的正确版本以及在哪里找到它的有用信息。
有没有人有任何想法我可以尝试解决这个错误?
A picture of the error

//-----------------------------------------------------------------------
// <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();

}
}
}

最佳答案

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

关于c# - 使用 Microsoft.PowerShell.Commands 找不到命名空间 'CopyItemCommand',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70246263/

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