Advertisement
NPSF3000

Bracket Counter (using Embedded Resources)

Oct 24th, 2011
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.IO;
  7. namespace ReadText
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             var cheese = GetTextResource("TextFile1.txt");
  15.             Console.WriteLine(cheese.Count(x => x == '{'));
  16.             Console.WriteLine(cheese.Count(x => x == '}'));
  17.             Console.ReadLine();
  18.         }
  19.  
  20.         public static string GetTextResource(string name)
  21.         {
  22.             Assembly assembly = Assembly.GetExecutingAssembly();
  23.             string contents = string.Empty;
  24.             try
  25.             {
  26.                 Stream strm = assembly.GetManifestResourceStream(assembly.GetName().Name.ToString() + "." + name);
  27.                 StreamReader reader = new StreamReader(strm);
  28.  
  29.                 contents += reader.ReadToEnd();
  30.  
  31.                 reader.Close();
  32.                 return contents;
  33.             }
  34.             catch (IOException ex)
  35.             {
  36.                 Console.WriteLine(ex.Message);
  37.                 return string.Empty;
  38.             }
  39.             catch (ArgumentNullException ex)
  40.             {
  41.                 Console.WriteLine(ex.Message);
  42.                 return string.Empty;
  43.             }
  44.         }
  45.  
  46.  
  47.     }
  48. }
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement