简体   繁体   中英

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

I am very new to the C# and .NET stuff and been experimenting with some HTML parsing and file downloading when i came across a problem. I had previously written my code in .NET Core and it was working fine and then i realised that i need to use System.Windows.Forms to be able to access User's monitor resolution and tried transfering my code to .NET framework app to be able to add the System.Windows.Forms assembly (Apparently it is not possible in .NET Core, Please let me know if I am wrong 😁). So Here is the code:

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]);
        }
    }
}
}

After copying and pasting the code in a new Console app with .NET framework I get errors in

var DLink = aTagContainer.Attributes["href"].Value.Split("Url=");

saying that inside Split it should be a Char and not a string! then why did it work in .NET Core?

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

Also the Download function seems to not like the way it is set up for some reason and will not work no matter what despite working fine in .NET Core.

Also I know my code is probably not as well-written, I would be happy to hear your criticism regarding that as well. Thank you in advance kind stranger 🥰

in .net core 2.0 and above, there is an overload for split which takes a string as input however this is not the case in .net framework which takes a character for split. If you want to split a string based on another string use the sample code mentioned below.

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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