.NET Core 与 .NET Framework,我的代码作为 .NET Core 的一部分运行但不作为 .NET Framework 的一部分运行,为什么?

.NET Core vs .NET Framework, My code works in as part of .NET Core but doesn't as part of .NET framework, Why?

我对 C# 和 .NET 非常陌生,并且在遇到问题时一直在尝试一些 HTML 解析和文件下载。 我之前在 .NET Core 中编写了我的代码并且它工作正常然后我意识到我需要使用 System.Windows.Forms 才能访问用户的监视器分辨率并尝试将我的代码传输到 .NET 框架应用程序以便能够添加 System.Windows.Forms 程序集(显然这在 .NET Core 中是不可能的,如果我错了请告诉我)。 所以这是代码:

using System;
using System.Linq;
using HtmlAgilityPack;
using System.Net;
using System.IO;

namespace AmazingSuperAwesomeWallpaperDownloaderAppConsoleApp
{
  class Program
  {
    static void Main(string[] args)
    {
        Download();
    }
    static void Download()
    {
        Console.WriteLine("Please enter the wallpaper page ID: ");
        //Asks for user to input the Wallpaper ID and creates the link out of it.
        var userInput = "https://SuperSecretWallpaperWebsite.com/Wallpapers/" + Console.ReadLine();

        // set the html var to the website link to parse
        var html = @userInput;

        //gets the website html from the HTTP
        HtmlWeb web = new HtmlWeb();

        //Creates a var called document to load the wesite in it.
        var document = web.Load(html);

        var fileName = "Wallpaper.jpg";
        //Creates a container, the first div to pass a certain criteria for the container will populate this container
        var container = document.DocumentNode.Descendants("div").FirstOrDefault(x => x.Attributes.Contains("class") && x.Attributes["class"].Value == "wallpaperContainer");
        if (container != null)
        {
            //Does the same thing as above and looks for the Div with details class and stores it in the title var.
            var titleContainer = container.Descendants("div").FirstOrDefault(x => x.Attributes.Contains("class") && x.Attributes["class"].Value == "details");
            if (title != null)
            {
                //Finds the first h1 tag that is in title var and stores it in titlevalue
                var titleValue = titleContainer.Descendants("h1").FirstOrDefault();
                if (titleValue != null)
                {
                    //A var that is set to the innter text of the the h1 that was found in the titleValue var.
                    var h1TitleValue = titleValue.InnerText;
                    //Adds file extension to the file name which is the title
                    fileName = h1TitleValue + ".jpg";
                    Console.WriteLine("Title: " + h1TitleValue);
                }

            }
        }
        //Creates aTag container, the first aTag to pass a certain criteria for the container will populate this container
        var aTagContainer = document.DocumentNode.Descendants("a").FirstOrDefault(x => x.Attributes.Contains("class") && x.Attributes["class"].Value == "downloadButton" && x.Attributes.Contains("href"));
        if (aTagContainer != null)
        {
            //Splits the wallpaper URL from the rest of the useless url stuff
            var DLink = aTagContainer.Attributes["href"].Value.Split("Url=");

            using (var client = new WebClient())
            {
                //Finds the MyPictures folder for the logged in user and sets it to the wallpaper directory
                var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "SuperAwesomeWallpaperDownloadFolder\");
                //Creates the folder in MyPicture folder
                System.IO.Directory.CreateDirectory(filePath);
                //Downloads the wallpaper file and saves it in the directory
                client.DownloadFile(DLink[1], (filePath + fileName));
                Console.WriteLine(filePath);
            }
            Console.WriteLine("Download Link is: " + DLink[1]);
        }
    }
}
}

在使用 .NET 框架的新控制台应用程序中复制并粘贴代码后,我在

中遇到错误
var DLink = aTagContainer.Attributes["href"].Value.Split("Url=");

说在 Split 里面它应该是一个 Char 而不是一个字符串!那为什么它在 .NET Core 中工作?

client.DownloadFile(DLink[1], (filePath + fileName));

此外,出于某种原因,下载功能似乎不喜欢它的设置方式,尽管在 .NET Core 中运行良好,但无论如何都无法运行。

我也知道我的代码可能写得不太好,我也很乐意听到您对此的批评。 提前谢谢你好心的陌生人

在 .net core 2.0 及更高版本中,split 有一个重载,它将字符串作为输入,但是在 .net 框架中情况并非如此,它采用字符进行拆分。如果您想根据另一个字符串拆分一个字符串,请使用下面提到的示例代码。

var DLink = aTagContainer.Attributes["href"].Value
              .Split(new string[] { "Url=" }, StringSplitOptions.None);

希望对您有所帮助。