FileFuns.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Security;
  10. using System.Security.Cryptography;
  11. using System.Security.Permissions;
  12. namespace SysSecLibs
  13. {
  14. public class FileFuns
  15. {
  16. // Methods
  17. public static void AddSeachingFolder(string pcSeachingFolder)
  18. {
  19. pcSeachingFolder = FileFuns.GetFolder(pcSeachingFolder, false);
  20. if (pcSeachingFolder != "" && !FileFuns.SeachingFolders.Contains(pcSeachingFolder))
  21. {
  22. FileFuns.SeachingFolders.Add(pcSeachingFolder);
  23. }
  24. }
  25. public static void Close()
  26. {
  27. if (FileFuns._Reader != null)
  28. {
  29. FileFuns._Reader.Close();
  30. }
  31. FileFuns._Reader = null;
  32. }
  33. private static bool DemainSecurity()
  34. {
  35. bool flag1 = true;
  36. try
  37. {
  38. new PrincipalPermission("qwertyuiopasdfgh", "qwertyuiopasdfgh").Demand();
  39. }
  40. catch (Exception)
  41. {
  42. flag1 = false;
  43. }
  44. return flag1;
  45. }
  46. public static bool FileExists(string pcFileName)
  47. {
  48. string text1 = FileFuns.GetFullPathFileName(pcFileName);
  49. return (text1 != "");
  50. }
  51. public static string GetAssemblyFolder()
  52. {
  53. FileInfo info1 = new FileInfo(Assembly.GetCallingAssembly().Location);
  54. return info1.DirectoryName;
  55. }
  56. public static string GetFolder(string pcFolder, bool plCreateFolder)
  57. {
  58. string text1 = pcFolder;
  59. text1 = text1.TrimEnd(new char[] { '\\' }) + @"\";
  60. if (Directory.Exists(text1))
  61. {
  62. return text1;
  63. }
  64. if (plCreateFolder)
  65. {
  66. try
  67. {
  68. Directory.CreateDirectory(text1);
  69. }
  70. catch (Exception)
  71. {
  72. text1 = "";
  73. }
  74. return text1;
  75. }
  76. return "";
  77. }
  78. public static string GetFullPathFileName(string pcFileName)
  79. {
  80. string text1 = "";
  81. if (File.Exists(pcFileName))
  82. {
  83. text1 = pcFileName;
  84. }
  85. else
  86. {
  87. foreach (string text2 in FileFuns.SeachingFolders)
  88. {
  89. string text3 = text2 + pcFileName;
  90. if (File.Exists(text3))
  91. {
  92. text1 = text3;
  93. break;
  94. }
  95. }
  96. }
  97. if (text1 != "")
  98. {
  99. FileInfo info1 = new FileInfo(text1);
  100. text1 = info1.FullName;
  101. }
  102. return text1;
  103. }
  104. private static bool Open(string pcFileName)
  105. {
  106. bool flag1 = false;
  107. if (FileFuns.DemainSecurity())
  108. {
  109. flag1 = FileFuns.Open(pcFileName, FileFuns._key);
  110. }
  111. return flag1;
  112. }
  113. private static bool Open(string pcFileName, string pcSecurityCode)
  114. {
  115. bool flag1 = false;
  116. FileFuns.Close();
  117. string text1 = FileFuns.GetFullPathFileName(pcFileName);
  118. if ((pcSecurityCode != "qwertyuiopasdfgh") || (text1 == ""))
  119. {
  120. return flag1;
  121. }
  122. byte[] buffer1 = Encoding.ASCII.GetBytes(FileFuns._key);
  123. byte[] buffer2 = Encoding.ASCII.GetBytes(FileFuns._IV);
  124. FileStream stream1 = new FileStream(text1, FileMode.Open, FileAccess.Read);
  125. SymmetricAlgorithm algorithm1 = SymmetricAlgorithm.Create();
  126. CryptoStream stream2 = new CryptoStream(stream1, algorithm1.CreateDecryptor(buffer1, buffer2), CryptoStreamMode.Read);
  127. MemoryStream stream3 = new MemoryStream();
  128. byte[] buffer3 = new byte[0x1388];
  129. int num1 = 0x1388;
  130. while (num1 == 0x1388)
  131. {
  132. num1 = stream2.Read(buffer3, 0, 0x1388);
  133. if (num1 > 0)
  134. {
  135. stream3.Write(buffer3, 0, num1);
  136. }
  137. }
  138. stream1.Close();
  139. stream2.Close();
  140. FileFuns._Reader = new StreamReader(stream3);
  141. return true;
  142. }
  143. public static string ReadAll()
  144. {
  145. string text1 = "";
  146. if (FileFuns._Reader != null)
  147. {
  148. ((MemoryStream)FileFuns._Reader.BaseStream).Seek((long)0, SeekOrigin.Begin);
  149. text1 = FileFuns._Reader.ReadToEnd();
  150. }
  151. return text1;
  152. }
  153. public static string ReadAll(string pcFileName)
  154. {
  155. string text1 = "";
  156. if (FileFuns.Open(pcFileName))
  157. {
  158. text1 = FileFuns.ReadAll();
  159. }
  160. return text1;
  161. }
  162. public static string ReadEncryptedFile(string pcFileName, string pcSecurityCode)
  163. {
  164. string text1 = "";
  165. string text2 = FileFuns.GetFullPathFileName(pcFileName);
  166. if (text2 != "")
  167. {
  168. byte[] buffer1 = Encoding.ASCII.GetBytes(pcSecurityCode);
  169. byte[] buffer2 = Encoding.ASCII.GetBytes(pcSecurityCode);
  170. FileStream stream1 = new FileStream(text2, FileMode.Open, FileAccess.Read);
  171. SymmetricAlgorithm algorithm1 = SymmetricAlgorithm.Create();
  172. CryptoStream stream2 = new CryptoStream(stream1, algorithm1.CreateDecryptor(buffer1, buffer2), CryptoStreamMode.Read);
  173. MemoryStream stream3 = new MemoryStream();
  174. byte[] buffer3 = new byte[0x1388];
  175. int num1 = 0x1388;
  176. while (num1 == 0x1388)
  177. {
  178. num1 = stream2.Read(buffer3, 0, 0x1388);
  179. if (num1 > 0)
  180. {
  181. stream3.Write(buffer3, 0, num1);
  182. }
  183. }
  184. stream1.Close();
  185. stream2.Close();
  186. StreamReader reader1 = new StreamReader(stream3);
  187. stream3.Seek((long)0, SeekOrigin.Begin);
  188. text1 = reader1.ReadToEnd();
  189. reader1.Close();
  190. }
  191. return text1;
  192. }
  193. public static string ReadFileAsString(string pcFileName)
  194. {
  195. string text1 = "";
  196. pcFileName = GetFullPathFileName(pcFileName);
  197. if (pcFileName != "")
  198. {
  199. StreamReader reader1 = new StreamReader(pcFileName, Encoding.Default);
  200. text1 = reader1.ReadToEnd();
  201. reader1.Close();
  202. }
  203. return text1;
  204. }
  205. public static string ReadSection(string pcSectionName)
  206. {
  207. string text1 = "";
  208. if (FileFuns._Reader != null)
  209. {
  210. string text2 = FileFuns.ReadAll();
  211. pcSectionName = "[" + pcSectionName + "]";
  212. int num1 = text2.ToUpper().IndexOf(pcSectionName.ToUpper());
  213. if (num1 >= 0)
  214. {
  215. num1 = text2.IndexOf("\r\n", num1);
  216. }
  217. if (num1 < 0)
  218. {
  219. return text1;
  220. }
  221. num1 += 2;
  222. FileFuns._Reader.BaseStream.Seek((long)num1, SeekOrigin.Begin);
  223. for (string text4 = FileFuns._Reader.ReadLine(); text4 != null; text4 = FileFuns._Reader.ReadLine())
  224. {
  225. if (((text4 != "") && (text4[0] == '[')) && (text4[text4.Length - 1] == ']'))
  226. {
  227. return text1;
  228. }
  229. text1 = text1 + ((text1 == "") ? "" : "\r\n") + text4;
  230. }
  231. }
  232. return text1;
  233. }
  234. public static string ReadSection(string pcSectionName, string pcFileName)
  235. {
  236. string text1 = "";
  237. if (FileFuns.Open(pcFileName))
  238. {
  239. text1 = FileFuns.ReadSection(pcSectionName);
  240. }
  241. return text1;
  242. }
  243. private static string ReadSection(string pcSectionName, string pcFileName, string pcSecurityCode)
  244. {
  245. string text1 = "";
  246. if (FileFuns.Open(pcFileName, pcSecurityCode))
  247. {
  248. text1 = FileFuns.ReadSection(pcSectionName);
  249. }
  250. return text1;
  251. }
  252. public static void SaveConnectionString(string pcConnStr)
  253. {
  254. if (pcConnStr != null && pcConnStr.Trim().Length > 0)
  255. {
  256. string lcFullName = FileFuns.GetFullPathFileName("RSDB.cfg");
  257. SaveToFile(pcConnStr, lcFullName);
  258. }
  259. }
  260. public static string ReadSystemLicense()
  261. {
  262. return ReadSection("MachineId", "License", "qwertyuiopasdfgh");
  263. }
  264. public static void RemoveSeachingFolder(string pcSeachingFolder)
  265. {
  266. if (FileFuns.SeachingFolders.Contains(pcSeachingFolder))
  267. {
  268. FileFuns.SeachingFolders.Remove(pcSeachingFolder);
  269. }
  270. }
  271. public static bool SaveImageToFile(string pcFileName, Image poImage)
  272. {
  273. bool flag1 = false;
  274. if (pcFileName == "")
  275. {
  276. return flag1;
  277. }
  278. ImageFormat format1 = ImageFormat.Bmp;
  279. FileInfo info1 = new FileInfo(pcFileName);
  280. switch (info1.Extension.ToUpper().Trim())
  281. {
  282. case "BMP":
  283. format1 = ImageFormat.Bmp;
  284. goto Label_0091;
  285. case "WMF":
  286. format1 = ImageFormat.Wmf;
  287. break;
  288. case "JPG":
  289. format1 = ImageFormat.Jpeg;
  290. break;
  291. }
  292. Label_0091:
  293. try
  294. {
  295. poImage.Save(pcFileName, format1);
  296. flag1 = true;
  297. }
  298. catch (Exception)
  299. {
  300. }
  301. return flag1;
  302. }
  303. /// <summary>
  304. /// 将字符串加密保存到指定的文件中
  305. /// </summary>
  306. /// <param name="pcStringToSave"></param>
  307. /// <param name="pcFileName"></param>
  308. public static void SaveToFile(string pcStringToSave, string pcFileName)
  309. {
  310. FileStream stream1 = new FileStream(pcFileName, FileMode.OpenOrCreate, FileAccess.Write);
  311. stream1.SetLength((long)0);
  312. byte[] buffer1 = Encoding.ASCII.GetBytes(FileFuns._key);
  313. byte[] buffer2 = Encoding.ASCII.GetBytes(FileFuns._IV);
  314. SymmetricAlgorithm algorithm1 = SymmetricAlgorithm.Create();
  315. CryptoStream stream2 = new CryptoStream(stream1, algorithm1.CreateEncryptor(buffer1, buffer2), CryptoStreamMode.Write);
  316. byte[] buffer3 = Encoding.ASCII.GetBytes(pcStringToSave);
  317. stream2.Write(buffer3, 0, buffer3.Length);
  318. stream1.Flush();
  319. stream2.Close();
  320. stream1.Close();
  321. }
  322. private static string TranslateFileFilter(string pcFileType)
  323. {
  324. string text2;
  325. string text1 = "All Files (*.*)|*.*";
  326. if ((text2 = pcFileType.ToUpper()) == null)
  327. {
  328. return text1;
  329. }
  330. text2 = string.IsInterned(text2);
  331. if (text2 == "DLL")
  332. {
  333. return "Library files (*.dll)|*.dll";
  334. }
  335. if (text2 == "NPC")
  336. {
  337. return "Configuration files (*.rsc)|*.rsc";
  338. }
  339. if (text2 == "XLS")
  340. {
  341. return "Excel Files (*.xls)|*.xls";
  342. }
  343. if (text2 != "IMAGE")
  344. {
  345. return text1;
  346. }
  347. return "Bitmap files (*.bmp)|*.bmp|JPG Files (*.jpg)|*.jpg|WMF Files (*.wmf)|*.wmf|All Files (*.*)|*.*";
  348. }
  349. public static bool WriteStringToFile(string pcStringToWrite, string pcFileName)
  350. {
  351. bool flag1 = true;
  352. if (File.Exists(pcFileName))
  353. {
  354. File.Delete(pcFileName);
  355. }
  356. StreamWriter writer1 = new StreamWriter(pcFileName);
  357. writer1.Write(pcStringToWrite);
  358. writer1.Flush();
  359. writer1.Close();
  360. return flag1;
  361. }
  362. /// <summary>
  363. /// 得到文件里的内容
  364. /// </summary>
  365. /// <param name="FilePath">文件</param>
  366. /// <returns>文件内容</returns>
  367. public static string GetStream(string FilePath)
  368. {
  369. FileStream stream1 = new FileStream(FilePath, FileMode.Open);
  370. byte[] buffer1 = new byte[Convert.ToInt32(stream1.Length)];
  371. stream1.Read(buffer1, 0, buffer1.Length);
  372. stream1.Close();
  373. return Convert.ToBase64String(buffer1);
  374. }
  375. // Properties
  376. public static ArrayList SeachingFolders
  377. {
  378. get
  379. {
  380. if (FileFuns._SeachingFolders == null)
  381. {
  382. FileFuns._SeachingFolders = new ArrayList();
  383. }
  384. return FileFuns._SeachingFolders;
  385. }
  386. }
  387. // Fields
  388. private static string _IV = "qwertyuiopasdfgh";
  389. private static string _key = "qwertyuiopasdfgh";
  390. private static StreamReader _Reader;
  391. private static ArrayList _SeachingFolders = new ArrayList();
  392. private const int BUFFERSIZE = 0x1388;
  393. }
  394. }