.NET Core vs .NET Framework,我的代码作为 .NET Core 的一部分工作,但不作为 .NET 框架的一部分,为什么?



我对C#和.NET很陌生,遇到问题时,我一直在尝试一些HTML解析和文件下载。我以前在.NET Core中编写过代码,它运行良好,然后我意识到我需要使用System.Windows.Forms才能访问用户的监视器解析,并尝试将我的代码转移到.NET framework应用程序,以便能够添加System.Windows.Form程序集😁).这是代码:

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=");

说在斯普利特里面应该是查尔而不是字符串!那么为什么它能在.NET Core中工作呢?

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

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

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

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

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

希望能有所帮助。

相关内容

最新更新