123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- using System;
- using System.Collections;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Reflection;
- using System.Security.Cryptography;
- using System.Security.Permissions;
- using System.Runtime.InteropServices;
- using SysBaseLibs;
- namespace SysBaseLib.Security
- {
- public class FileFuns
- {
- /// <summary>
- /// 判断文件是否已经打开 Open
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- static public bool IsFileOpen(string filePath)
- {
- if (!File.Exists(filePath))
- {
- ThreadLog.LogErr(filePath + "文件都不存在!");
- return true;
- }
- IntPtr vHandle = _lopen(filePath, OF_READWRITE | OF_SHARE_DENY_NONE);
- if (vHandle == HFILE_ERROR)
- {
- ThreadLog.LogErr(filePath + "文件被占用!");
- return true;
- }
- CloseHandle(vHandle);
- ThreadLog.LogInfo(filePath + "没有被占用!");
- return false;
- }
- /// <summary>
- /// 文件正在被使用
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static bool IsFileInUse(string fileName)
- {
- bool inUse = true;
- FileStream fs = null;
- try
- {
- fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
- inUse = false;
- }
- finally
- {
- if (fs != null)
- fs.Close();
- }
- return inUse;//true表示正在使用,false没有使用
- }
- // Methods
- public static void AddSeachingFolder(string pcSeachingFolder)
- {
- pcSeachingFolder = FileFuns.GetFolder(pcSeachingFolder, false);
- if (pcSeachingFolder != "" && !FileFuns.SeachingFolders.Contains(pcSeachingFolder))
- {
- FileFuns.SeachingFolders.Add(pcSeachingFolder);
- }
- }
- 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)
- {
- return ReadEncryptedFile(pcFileName, _IV);
- }
- 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 != "")
- {
- FileStream fs = new FileStream(pcFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- StreamReader reader = new StreamReader(fs);
- //StreamReader reader = new StreamReader(pcFileName, Encoding.UTF8);
- text1 = reader.ReadToEnd();
- reader.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);
- }
- }
- /// <summary>
- /// 将图片保存到指定的文件中
- /// </summary>
- /// <param name="pcFileName"></param>
- /// <param name="poImage"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 将字符串加密保存到指定的文件中
- /// </summary>
- /// <param name="pcStringToSave"></param>
- /// <param name="pcFileName"></param>
- 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 == "RSC")
- // {
- // 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 (*.*)|*.*";
- //}
- /// <summary>
- /// 将字符串内容写入到文件中
- /// </summary>
- /// <param name="pcStringToWrite">需要写入的字符串内容</param>
- /// <param name="pcFileName">文件名称</param>
- /// <returns></returns>
- public static bool WriteStringToFile(string pcStringToWrite, string pcFileName)
- {
- bool flag1 = true;
- if (File.Exists(pcFileName))
- {
- File.Delete(pcFileName);
- }
- StreamWriter writer1 = new StreamWriter(pcFileName, false, Encoding.UTF8);
- writer1.Write(pcStringToWrite);
- writer1.Flush();
- writer1.Close();
- return flag1;
- }
- /// <summary>
- /// 得到文件里的内容
- /// </summary>
- /// <param name="FilePath">文件</param>
- /// <returns>文件内容</returns>
- 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;
- }
- }
- public static void Close()
- {
- if (FileFuns._Reader != null)
- {
- FileFuns._Reader.Close();
- }
- FileFuns._Reader = null;
- }
- // Fields
- [DllImport("kernel32.dll")]
- public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
- [DllImport("kernel32.dll")]
- public static extern bool CloseHandle(IntPtr hObject);
- const int OF_READWRITE = 2;
- const int OF_SHARE_DENY_NONE = 0x40;
- const int BUFFERSIZE = 0x1388;
- static readonly IntPtr HFILE_ERROR = new IntPtr(-1);
- static string _IV = "qwertyuiopasdfgh";
- static string _key = "qwertyuiopasdfgh";
- static StreamReader _Reader;
- static ArrayList _SeachingFolders = new ArrayList();
- }
- }
|