using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Reflection; using System.Security; using System.Security.Cryptography; using System.Security.Permissions; namespace SysSecLibs { public class FileFuns { // Methods public static void AddSeachingFolder(string pcSeachingFolder) { pcSeachingFolder = FileFuns.GetFolder(pcSeachingFolder, false); if (pcSeachingFolder != "" && !FileFuns.SeachingFolders.Contains(pcSeachingFolder)) { FileFuns.SeachingFolders.Add(pcSeachingFolder); } } public static void Close() { if (FileFuns._Reader != null) { FileFuns._Reader.Close(); } FileFuns._Reader = null; } private static bool DemainSecurity() { bool flag1 = true; try { new PrincipalPermission("qwertyuiopasdfgh", "qwertyuiopasdfgh").Demand(); } catch (Exception) { flag1 = false; } return flag1; } public static bool FileExists(string pcFileName) { string text1 = FileFuns.GetFullPathFileName(pcFileName); return (text1 != ""); } public static string GetAssemblyFolder() { FileInfo info1 = new FileInfo(Assembly.GetCallingAssembly().Location); return info1.DirectoryName; } public static string GetFolder(string pcFolder, bool plCreateFolder) { string text1 = pcFolder; text1 = text1.TrimEnd(new char[] { '\\' }) + @"\"; if (Directory.Exists(text1)) { return text1; } if (plCreateFolder) { try { Directory.CreateDirectory(text1); } catch (Exception) { text1 = ""; } return text1; } return ""; } public static string GetFullPathFileName(string pcFileName) { string text1 = ""; if (File.Exists(pcFileName)) { text1 = pcFileName; } else { foreach (string text2 in FileFuns.SeachingFolders) { string text3 = text2 + pcFileName; if (File.Exists(text3)) { text1 = text3; break; } } } if (text1 != "") { FileInfo info1 = new FileInfo(text1); text1 = info1.FullName; } return text1; } private static bool Open(string pcFileName) { bool flag1 = false; if (FileFuns.DemainSecurity()) { flag1 = FileFuns.Open(pcFileName, FileFuns._key); } return flag1; } private static bool Open(string pcFileName, string pcSecurityCode) { bool flag1 = false; FileFuns.Close(); string text1 = FileFuns.GetFullPathFileName(pcFileName); if ((pcSecurityCode != "qwertyuiopasdfgh") || (text1 == "")) { return flag1; } byte[] buffer1 = Encoding.ASCII.GetBytes(FileFuns._key); byte[] buffer2 = Encoding.ASCII.GetBytes(FileFuns._IV); FileStream stream1 = new FileStream(text1, FileMode.Open, FileAccess.Read); SymmetricAlgorithm algorithm1 = SymmetricAlgorithm.Create(); CryptoStream stream2 = new CryptoStream(stream1, algorithm1.CreateDecryptor(buffer1, buffer2), CryptoStreamMode.Read); MemoryStream stream3 = new MemoryStream(); byte[] buffer3 = new byte[0x1388]; int num1 = 0x1388; while (num1 == 0x1388) { num1 = stream2.Read(buffer3, 0, 0x1388); if (num1 > 0) { stream3.Write(buffer3, 0, num1); } } stream1.Close(); stream2.Close(); FileFuns._Reader = new StreamReader(stream3); return true; } public static string ReadAll() { string text1 = ""; if (FileFuns._Reader != null) { ((MemoryStream)FileFuns._Reader.BaseStream).Seek((long)0, SeekOrigin.Begin); text1 = FileFuns._Reader.ReadToEnd(); } return text1; } public static string ReadAll(string pcFileName) { string text1 = ""; if (FileFuns.Open(pcFileName)) { text1 = FileFuns.ReadAll(); } return text1; } public static string ReadEncryptedFile(string pcFileName, string pcSecurityCode) { string text1 = ""; string text2 = FileFuns.GetFullPathFileName(pcFileName); if (text2 != "") { byte[] buffer1 = Encoding.ASCII.GetBytes(pcSecurityCode); byte[] buffer2 = Encoding.ASCII.GetBytes(pcSecurityCode); FileStream stream1 = new FileStream(text2, FileMode.Open, FileAccess.Read); SymmetricAlgorithm algorithm1 = SymmetricAlgorithm.Create(); CryptoStream stream2 = new CryptoStream(stream1, algorithm1.CreateDecryptor(buffer1, buffer2), CryptoStreamMode.Read); MemoryStream stream3 = new MemoryStream(); byte[] buffer3 = new byte[0x1388]; int num1 = 0x1388; while (num1 == 0x1388) { num1 = stream2.Read(buffer3, 0, 0x1388); if (num1 > 0) { stream3.Write(buffer3, 0, num1); } } stream1.Close(); stream2.Close(); StreamReader reader1 = new StreamReader(stream3); stream3.Seek((long)0, SeekOrigin.Begin); text1 = reader1.ReadToEnd(); reader1.Close(); } return text1; } public static string ReadFileAsString(string pcFileName) { string text1 = ""; pcFileName = GetFullPathFileName(pcFileName); if (pcFileName != "") { StreamReader reader1 = new StreamReader(pcFileName, Encoding.Default); text1 = reader1.ReadToEnd(); reader1.Close(); } return text1; } public static string ReadSection(string pcSectionName) { string text1 = ""; if (FileFuns._Reader != null) { string text2 = FileFuns.ReadAll(); pcSectionName = "[" + pcSectionName + "]"; int num1 = text2.ToUpper().IndexOf(pcSectionName.ToUpper()); if (num1 >= 0) { num1 = text2.IndexOf("\r\n", num1); } if (num1 < 0) { return text1; } num1 += 2; FileFuns._Reader.BaseStream.Seek((long)num1, SeekOrigin.Begin); for (string text4 = FileFuns._Reader.ReadLine(); text4 != null; text4 = FileFuns._Reader.ReadLine()) { if (((text4 != "") && (text4[0] == '[')) && (text4[text4.Length - 1] == ']')) { return text1; } text1 = text1 + ((text1 == "") ? "" : "\r\n") + text4; } } return text1; } public static string ReadSection(string pcSectionName, string pcFileName) { string text1 = ""; if (FileFuns.Open(pcFileName)) { text1 = FileFuns.ReadSection(pcSectionName); } return text1; } private static string ReadSection(string pcSectionName, string pcFileName, string pcSecurityCode) { string text1 = ""; if (FileFuns.Open(pcFileName, pcSecurityCode)) { text1 = FileFuns.ReadSection(pcSectionName); } return text1; } public static void SaveConnectionString(string pcConnStr) { if (pcConnStr != null && pcConnStr.Trim().Length > 0) { string lcFullName = FileFuns.GetFullPathFileName("RSDB.cfg"); SaveToFile(pcConnStr, lcFullName); } } public static string ReadSystemLicense() { return ReadSection("MachineId", "License", "qwertyuiopasdfgh"); } public static void RemoveSeachingFolder(string pcSeachingFolder) { if (FileFuns.SeachingFolders.Contains(pcSeachingFolder)) { FileFuns.SeachingFolders.Remove(pcSeachingFolder); } } public static bool SaveImageToFile(string pcFileName, Image poImage) { bool flag1 = false; if (pcFileName == "") { return flag1; } ImageFormat format1 = ImageFormat.Bmp; FileInfo info1 = new FileInfo(pcFileName); switch (info1.Extension.ToUpper().Trim()) { case "BMP": format1 = ImageFormat.Bmp; goto Label_0091; case "WMF": format1 = ImageFormat.Wmf; break; case "JPG": format1 = ImageFormat.Jpeg; break; } Label_0091: try { poImage.Save(pcFileName, format1); flag1 = true; } catch (Exception) { } return flag1; } /// /// 将字符串加密保存到指定的文件中 /// /// /// public static void SaveToFile(string pcStringToSave, string pcFileName) { FileStream stream1 = new FileStream(pcFileName, FileMode.OpenOrCreate, FileAccess.Write); stream1.SetLength((long)0); byte[] buffer1 = Encoding.ASCII.GetBytes(FileFuns._key); byte[] buffer2 = Encoding.ASCII.GetBytes(FileFuns._IV); SymmetricAlgorithm algorithm1 = SymmetricAlgorithm.Create(); CryptoStream stream2 = new CryptoStream(stream1, algorithm1.CreateEncryptor(buffer1, buffer2), CryptoStreamMode.Write); byte[] buffer3 = Encoding.ASCII.GetBytes(pcStringToSave); stream2.Write(buffer3, 0, buffer3.Length); stream1.Flush(); stream2.Close(); stream1.Close(); } private static string TranslateFileFilter(string pcFileType) { string text2; string text1 = "All Files (*.*)|*.*"; if ((text2 = pcFileType.ToUpper()) == null) { return text1; } text2 = string.IsInterned(text2); if (text2 == "DLL") { return "Library files (*.dll)|*.dll"; } if (text2 == "NPC") { return "Configuration files (*.rsc)|*.rsc"; } if (text2 == "XLS") { return "Excel Files (*.xls)|*.xls"; } if (text2 != "IMAGE") { return text1; } return "Bitmap files (*.bmp)|*.bmp|JPG Files (*.jpg)|*.jpg|WMF Files (*.wmf)|*.wmf|All Files (*.*)|*.*"; } public static bool WriteStringToFile(string pcStringToWrite, string pcFileName) { bool flag1 = true; if (File.Exists(pcFileName)) { File.Delete(pcFileName); } StreamWriter writer1 = new StreamWriter(pcFileName); writer1.Write(pcStringToWrite); writer1.Flush(); writer1.Close(); return flag1; } /// /// 得到文件里的内容 /// /// 文件 /// 文件内容 public static string GetStream(string FilePath) { FileStream stream1 = new FileStream(FilePath, FileMode.Open); byte[] buffer1 = new byte[Convert.ToInt32(stream1.Length)]; stream1.Read(buffer1, 0, buffer1.Length); stream1.Close(); return Convert.ToBase64String(buffer1); } // Properties public static ArrayList SeachingFolders { get { if (FileFuns._SeachingFolders == null) { FileFuns._SeachingFolders = new ArrayList(); } return FileFuns._SeachingFolders; } } // Fields private static string _IV = "qwertyuiopasdfgh"; private static string _key = "qwertyuiopasdfgh"; private static StreamReader _Reader; private static ArrayList _SeachingFolders = new ArrayList(); private const int BUFFERSIZE = 0x1388; } }