Advertisement
nevachana

Filter c#

Jun 16th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 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.  
  8. namespace Filtering
  9. {
  10.     class Program
  11.     {
  12.         public List<string> items = new List<string>();
  13.         static void Main(string[] args)
  14.         {
  15.             Console.Title = "Filter";
  16.             Program c = new Program();
  17.             c.setup();
  18.         }
  19.         public void setup()
  20.         {
  21.             this.write("Type filename to filter\nExample: proxys.txt");
  22.             string filename = Directory.GetCurrentDirectory().ToString() +@"\" +Console.ReadLine(); //we set the name of the file that we want to filter.
  23.             this.filter(filename);
  24.         }
  25.         public void filter(string filename)
  26.         {
  27.             try
  28.             {
  29.                 using (StreamReader sr = File.OpenText(filename))
  30.                 {
  31.                     string lines = sr.ReadToEnd(); // we read all the lines of the file
  32.                     string[] line = lines.Split('\n'); // we parse all the lanes by new lines.
  33.                     foreach (string filtering in line)
  34.                     {
  35.                         if(!items.Contains(filtering))//we check if our list contains the lane,if not then add it to the list
  36.                         items.Add(filtering);
  37.                     }
  38.                 }
  39.          
  40.                 this.write("File has been succesfully filtered");
  41.                 string randomfilename = randomString(); // we generate a random filename
  42.                 File.Create(randomfilename).Dispose();
  43.                 using (TextWriter tw = new StreamWriter(randomfilename))
  44.                 {
  45.                     for (int x = 0; x < this.items.Count; x++)
  46.                     {
  47.                         tw.WriteLine(this.items[x]); // we add all the lanes of the list to the new file
  48.                     }
  49.                         tw.Close();
  50.                 }
  51.                 this.write(string.Format("Everything is done! file has been saved as: {0}", randomfilename));
  52.                 Console.ReadLine();
  53.  
  54.             }
  55.             catch
  56.             {
  57.                 this.write(string.Format("Unable to find: {0}",filename));
  58.                 Console.ReadLine();
  59.             }
  60.         }
  61.         public void write(string txt)
  62.         {
  63.             Console.WriteLine(string.Format("{0} -- {1}", DateTime.Now, txt));
  64.         }
  65.         public string randomString()
  66.         {
  67.             var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  68.             var random = new Random();
  69.             var result = new string(
  70.                 Enumerable.Repeat(chars, 8)
  71.                           .Select(s => s[random.Next(s.Length)])
  72.                           .ToArray());
  73.             return result.ToString()+".txt";
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement