Exemple #1
0
 /// <summary>
 /// 从一个文件中字节系列
 /// </summary>
 /// <param name="filePath"></param>
 /// <returns></returns>
 public static byte[] ReadArray(string filePath)
 {
     if (File.Exists(filePath))
     {
         try
         {
             using (FileStream stream = FileOperator.OpenAsReadOnly(filePath))
             {
                 byte[] buffer = new byte[stream.Length];
                 stream.Read(buffer, 0, buffer.Length);
                 return(buffer);
             }
         }
         catch { }
     }
     return(new byte[0]);
 }
Exemple #2
0
        /// <summary>
        /// 将一个文件压缩为目标文件
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="targetFile"></param>
        /// <returns></returns>
        public static bool Compressed(string sourceFile, string targetFile)
        {
            if (!File.Exists(sourceFile))
            {
                return(false);
            }

            using (Stream target = new FileStream(targetFile, FileMode.Create, FileAccess.Write))
            {
                using (Stream source = FileOperator.OpenAsReadOnly(sourceFile))
                {
                    Compressed(source, target);
                }
            }
            FileOperator.SetFileVersion(targetFile, FileOperator.GetFileVersion(sourceFile));
            return(true);
        }
Exemple #3
0
 /// <summary>
 /// 从一个文件中加载可序列化对象
 /// </summary>
 /// <param name="filePath"></param>
 /// <returns></returns>
 public static object Read <T>(string filePath)
 {
     if (File.Exists(filePath))
     {
         try
         {
             using (FileStream fileStream = FileOperator.OpenAsReadOnly(filePath))
             {
                 XmlSerializer sr = new XmlSerializer(typeof(T));
                 return(sr.Deserialize(fileStream));
             }
         }
         catch { }
     }
     else
     {
     }
     return(null);
 }