Advertisement
Guest User

String Comperison

a guest
Jan 20th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using HtmlAgilityPack;
  9.  
  10. namespace TestConsole
  11. {
  12.     internal class Program
  13.     {
  14.  
  15.         static string OpenFile()
  16.         {
  17.             string path = string.Empty;
  18.             OpenFileDialog ofd = new OpenFileDialog();
  19.  
  20.             DialogResult result = ofd.ShowDialog();
  21.  
  22.             if(result == DialogResult.OK)
  23.             {
  24.                 path = ofd.FileName;
  25.             }
  26.  
  27.             if(path == "")
  28.             {
  29.                 Console.WriteLine("Something went wrong");
  30.             }
  31.             return path;
  32.         }
  33.  
  34.         [STAThread]
  35.         static void Main(string[] args)
  36.         {
  37.             try
  38.             {
  39.                 //get HTML Path
  40.                 Console.WriteLine("Please Select Modpack file the (.html) file: ");
  41.                 string htmlFilePath = OpenFile();
  42.  
  43.                 //get TXT Path
  44.                 Console.WriteLine("Please Select Server Installed mods file the (.txt) file: ");
  45.                 string textFilePath = OpenFile();
  46.  
  47.                 // Step 1: Load and parse the HTML file to extract data from <td data-type="DisplayName">
  48.                 HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
  49.                 htmlDocument.Load(htmlFilePath);
  50.                 var tdNodes = htmlDocument.DocumentNode.SelectNodes("//td[@data-type='DisplayName']");
  51.                 string[] htmlDataArray = tdNodes != null
  52.                     ? tdNodes.Select(node => node.InnerText.Trim()).ToArray()
  53.                     : Array.Empty<string>();
  54.  
  55.                 Console.WriteLine("Extracted Data from HTML File:");
  56.  
  57.                 // Step 2: Read the text file and load its content into an array
  58.                 string[] txtDataArray = File.ReadAllLines(textFilePath);
  59.                 Console.WriteLine("Extracted Data from Text File:");
  60.  
  61.                 // Step 3: Compare elements and create a new array with matches
  62.                 var matchingElements = htmlDataArray
  63.                     .Where(htmlItem => txtDataArray.Any(txtItem => txtItem.Contains(htmlItem)))
  64.                     .Select(htmlItem =>
  65.                     {
  66.                         // Find the matching text file line and rename the HTML item accordingly
  67.                         var matchingLine = txtDataArray.FirstOrDefault(txtItem => txtItem.Contains(htmlItem));
  68.                         return matchingLine ?? htmlItem; // Use the matching line or keep the HTML item
  69.                     })
  70.                     .Select(item => item + ";")
  71.                     .ToArray();
  72.  
  73.                 // Step 4: Output the results
  74.                 Console.WriteLine("Generated Output with Matches:");
  75.  
  76.                 string outPutString = string.Join("", matchingElements);
  77.                 Console.WriteLine(outPutString);
  78.  
  79.                 Console.WriteLine();
  80.                 // Step 5: Copy Output to clipboard.
  81.                 Clipboard.SetText(outPutString);
  82.                 Console.WriteLine("\nThe output has been copied to the clipboard!");
  83.                 Console.ReadKey();
  84.             }
  85.             catch (Exception ex)
  86.             {
  87.                 Console.WriteLine(ex.Message);
  88.             }
  89.         }
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement