我在PowerShell中遇到了.Net webclient对象的问题。
如果我想下载像https://example.ch/15.01.2015 something.docx
这样的文件,由于文件名中有多个.
,它无法正常工作。
这样的问题在Powershell webclient trailing dot in URL bug中有描述,但区别在于:文件夹中有.
。
我从那里尝试了代码,但它也没有用。我的代码是:
$source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Net;
public class FixedWebClient
{
public static System.Net.WebClient NewWebClient()
{
MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
foreach (string scheme in new[] { "http", "https" })
{
UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
if (parser != null)
{
int flagsValue = (int)flagsField.GetValue(parser);
// Clear the CanonicalizeAsFilePath attribute
if ((flagsValue & 0x1000000) != 0){
flagsField.SetValue(parser, flagsValue & ~0x1000000);
}
}
}
}
return new System.Net.WebClient();
}
}
"@
$file = "https://example.ch/15.01.2015 something.docx"
$target = "C:\temp\15.01.2015 something.docx"
#Remove-Typedata 'FixedWebClient'
Add-Type -TypeDefinition $source
$client = [FixedWebClient]::NewWebClient()
$client.UseDefaultCredentials=$true
$client.DownloadFile( $file , $target )
有谁知道,我怎么能解决我的问题?