SHsuperCM

C# Code Bank

Nov 28th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 0 0
  1. /*
  2.     SHsuperCM's C# Code Bank
  3.    
  4.     This file contains some code for the C# language that
  5.     anyone can use and not credit, and is organized using
  6.     a seperate C# program which is private.
  7. */
  8.  
  9. $utility start
  10. $$name start
  11. M - Async Key Down/Press
  12. $$name end
  13. $$information start
  14. These 2 Methods will tell you if a key is being held or being pressed.
  15.  
  16. GetAsyncKeyDown(Keys key) returns a boolean stating
  17. if "key" is down.
  18. GetAsyncKeyPress(Keys key) returns a boolean stating
  19. if "key" is down and was not down before.
  20. $$information end
  21. $$code start
  22. [DllImport("user32.dll")]
  23. private static extern short GetAsyncKeyState(int vKey);
  24. private static bool GetAsyncKeyDown(Keys key) { return GetAsyncKeyState((int)key) != 0; }
  25. private static List<Keys> prevKeysDown = new List<Keys>();
  26. private static bool GetAsyncKeyPress(Keys key)
  27. {
  28.     bool returning = true;
  29.     foreach (Keys prevKey in prevKeysDown){
  30.         if (key == prevKey) returning = false;
  31.     }
  32.     if (GetAsyncKeyDown(key) && returning == true) prevKeysDown.Add(key);
  33.     if (!GetAsyncKeyDown(key)) returning = false;
  34.     for (int i = 0; i < prevKeysDown.Count(); i++){
  35.         if (!GetAsyncKeyDown(prevKeysDown[i])) prevKeysDown.RemoveAt(i);
  36.     }
  37.     return returning;
  38. }
  39. $$code end
  40. $utility end
  41.  
  42. $utility start
  43. $$name start
  44. M - Stream from String
  45. $$name end
  46. $$information start
  47. Can be used to read a string line by line using a StreamReader.ReadLine().
  48.  
  49. StreamFromString(string strIN) returning
  50. a Stream from "strIN".
  51. $$information end
  52. $$code start
  53. public static Stream StreamFromString(string strIN)
  54. {
  55.     MemoryStream stream = new MemoryStream();
  56.     StreamWriter writer = new StreamWriter(stream);
  57.     writer.Write(strIN);
  58.     writer.Flush();
  59.     stream.Position = 0;
  60.     return stream;
  61. }
  62. $$code end
  63. $utility end
  64.  
  65. $utility start
  66. $$name start
  67. M - Stream from URL
  68. $$name end
  69. $$information start
  70. Creates a Stream from "urlIN"'s source code, also reads raw pastebins posts.
  71.  
  72. StreamFromURL(string urlIN) returning
  73. a Stream from "urlIN"'s URL.
  74. $$information end
  75. $$code start
  76. public static Stream StreamFromURL(string urlIN)
  77. {
  78.     return (new WebClient()).OpenRead(urlIN);
  79. }
  80. $$code end
  81. $utility end
  82.  
  83. $utility start
  84. $$name start
  85. M - Get Cut Part
  86. $$name end
  87. $$information start
  88. Get the string between 2 strings inside a big string.
  89.  
  90.  
  91. string GetCutPart(string inputIN, string startIN, string endIN, bool keepStartAndEndIN) returning
  92. a string that is inside "inputIN" and between "startIN" and "endIN",
  93. if "keepStartAndEndIN" is true, it will keep "startIN" and "endIN" on the returned value.
  94. $$information end
  95. $$code start
  96. public static string GetCutPart(string inputIN, string startIN, string endIN, bool keepStartAndEndIN)
  97. {
  98.     string returning = "";
  99.     string read = "";
  100.     bool collecting = false;
  101.  
  102.     foreach (char chr in inputIN)
  103.         if (collecting)
  104.         {
  105.             returning += chr;
  106.             if (returning.EndsWith(endIN))
  107.             {
  108.                 return (keepStartAndEndIN ? (startIN + returning) : returning.Replace(endIN, ""));
  109.             }
  110.         }
  111.         else
  112.         {
  113.             read += chr;
  114.             if (read.EndsWith(startIN))
  115.                 collecting = true;
  116.         }
  117.     return null;
  118. }
  119. $$code end
  120. $utility end
  121.  
  122. $utility start
  123. $$name start
  124. L - Open URL in browser
  125. $$name end
  126. $$information start
  127. Open URL inside the default browser.
  128. (chrome/internetexplorer/firefox...)
  129. $$information end
  130. $$code start
  131. Process.Start(string URL);
  132. $$code end
  133. $utility end
  134.  
  135. $utility start
  136. $$name start
  137. L - Set the ClipBoard text
  138. $$name end
  139. $$information start
  140. Set the text that will be in ctrl-v.
  141. $$information end
  142. $$code start
  143. Clipboard.SetText(string Text);
  144. $$code end
  145. $utility end
  146.  
  147. $utility start
  148. $$name start
  149. M - StreamReader From File Path
  150. $$name end
  151. $$information start
  152. Gets a StreamReader from the contents of the file in "pathIN".
  153.  
  154.  
  155. StreamReaderFromFile(string pathIN) returns
  156. StreamReader generated from the contents of the file in "pathIN",
  157. will return null if either the file does not exist or there was a problem reading it.
  158. $$information end
  159. $$code start
  160. public static StreamReader StreamReaderFromFile(string pathIN)
  161. {
  162.     if (File.Exists(pathIN))
  163.         try
  164.         {
  165.             return new StreamReader(pathIN);
  166.         }
  167.         catch { return null; }
  168.     else
  169.         return null;
  170. }
  171. $$code end
  172. $utility end
  173.  
  174. $utility start
  175. $$name start
  176. M - Search in list of strings
  177. $$name end
  178. $$information start
  179. Searching in a list of strings.
  180.  
  181. List<string> Search(List<string> input, string compared)
  182. returns a list of strings from input that contain the compared strings.
  183. $$information end
  184. $$code start
  185. public static List<string> Search(List<string> input, string compared)
  186. {
  187.     List<string> result = new List<string>();
  188.     foreach (string item in input)
  189.         if (item.Contains(compared))
  190.             result.Add(item);
  191.     return result;
  192. }
  193. $$code end
  194. $utility end
  195.  
  196. $utility start
  197. $$name start
  198. M - Is Form Active
  199. $$name end
  200. $$information start
  201. Determines if the form of which the method is located at is active.
  202. Active forms are the ones windows focuses on when choosing where user input will go.
  203. $$information end
  204. $$code start
  205. public bool IsActive() {
  206.     return System.Windows.Forms.Form.ActiveForm == this;
  207. }
  208. $$code end
  209. $utility end
Add Comment
Please, Sign In to add comment