Advertisement
gigahf

OSBuddy.cs

Aug 25th, 2017
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1.  
  2. // Invented by gigajew
  3. namespace Recovery.RuneScape {
  4.     public static class OSBuddy {
  5.         private static string UsernameFile = Environment.GetEnvironmentVariable("USERPROFILE") + "\\OSBuddy\\settings\\Remember Login Name.settings";
  6.        
  7.         public static string GetUsername() {
  8.             var fileInfo = new FileInfo(UsernameFile);
  9.             if(fileInfo.Exists) {
  10.                 using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
  11.                     using (StreamReader sr = new StreamReader(fs)) {
  12.                         var line = sr.ReadLine();
  13.                         var startIndex = IndexOfAny(line, (char) 34, 18) + 1;
  14.                         var endIndex = line.IndexOf((char) 34, startIndex + 1);
  15.                         return line.Substring(startIndex, endIndex - startIndex);
  16.                     }
  17.                 }
  18.             }
  19.             return default(string);
  20.         }
  21.        
  22.         private static int IndexOfAny(string haystack, char needle, int offset = 0) {
  23.             var index = haystack.IndexOf(needle);
  24.             for (int i = 0; i < offset; i++) {
  25.                 index = haystack.IndexOf(needle, index + 1);
  26.             }
  27.             return index;
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement