FileFuns.cs 17 KB

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