Пример #1
0
        private static void AssertFileSize(string fnWithSize)
        {
            long     fileSizeFromFilename = SafetyFile.GetFileSizeFromFilename(fnWithSize);
            FileInfo fileInfo             = new FileInfo(fnWithSize);

            Trace.Assert(fileSizeFromFilename == fileInfo.Length, "AssertFileSize,文件大小不等");
        }
Пример #2
0
 public static FileStream Open(string fn)
 {
     if (!File.Exists(fn))
     {
         SafetyFile.TryRestoreFile(fn);
     }
     return(File.Exists(fn) ? new FileStream(fn, FileMode.Open) : null);
 }
Пример #3
0
 public static void Save(string s, string fn)
 {
     byte[] bytes = Util.EncodingGb2312.GetBytes(s);
     using (var ms = new MemoryStream(bytes))
     {
         SafetyFile.Save(ms, fn);
     }
 }
Пример #4
0
 private static bool TryRestoreFile(string fn)
 {
     if (SafetyFile.RestoreFromFileNewSize(fn))
     {
         return(true);
     }
     if (SafetyFile.RestoreFromFileOldSize(fn))
     {
         return(true);
     }
     return(false);
 }
Пример #5
0
 public static void SerializeToFileWithFileName(object obj, string fn, bool isEncrypt = true)
 {
     if (obj != null)
     {
         string text = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented, Serializer._settings);
         if (isEncrypt)
         {
             text = Crypto.Encrypt(text);
         }
         SafetyFile.Save(text, fn);
     }
 }
Пример #6
0
        public static string ReadAll(string fn)
        {
            var fs = SafetyFile.Open(fn);

            if (fs == null)
            {
                return(null);
            }

            using (var read = new StreamReader(fs, Util.EncodingGb2312))
            {
                return(read.ReadToEnd());
            }
        }
Пример #7
0
 private static bool RestoreFromFileWithSize(string fn, string init)
 {
     try
     {
         string startWithFn = SafetyFile.GetFilenameStartWith(fn + init);
         if (string.IsNullOrEmpty(startWithFn))
         {
             return(false);
         }
         SafetyFile.AssertFileSize(startWithFn);
         File.Copy(startWithFn, fn);
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Пример #8
0
        public static T DeserializeFromFileWithFilename <T>(string fn, bool isEncrypted = true)
        {
            T t = default(T);

            try
            {
                var text = SafetyFile.ReadAll(fn);
                if (!string.IsNullOrEmpty(text))
                {
                    if (isEncrypted)
                    {
                        text = Crypto.Decrypt(text);
                    }
                    t = JsonConvert.DeserializeObject <T>(text, Serializer._settings);
                }
            }
            catch (Exception e)
            {
                Log.Exception(e);
            }
            return(t);
        }
Пример #9
0
 public static void Save(Stream stm, string fn)
 {
     if (File.Exists(fn))
     {
         var newName = SafetyFile.GetNewName(fn);
         SafetyFile.WriteStreamToFile(stm, newName);
         SafetyFile.DeleteFilesNameStartWith(newName + "_");
         var fi         = new FileInfo(newName);
         var newSplitFn = newName + "_" + fi.Length;
         FileEx.ReName(newName, newSplitFn);
         fi = new FileInfo(fn);
         string oldName = SafetyFile.GetOldName(fn) + "_";
         SafetyFile.DeleteFilesNameStartWith(oldName);
         string newfn = oldName + fi.Length;
         if (SafetyFile.IsFileStreamWithName(stm, fn))
         {
             stm.Close();
         }
         FileEx.ReName(fn, newfn);
         SafetyFile.AssertFileSize(newSplitFn);
         FileEx.ReName(newSplitFn, fn);
     }
     else
     {
         int idx = fn.LastIndexOf('\\');
         if (idx > 0)
         {
             Directory.CreateDirectory(fn.Substring(0, idx));
         }
         SafetyFile.WriteStreamToFile(stm, fn);
         if (SafetyFile.IsFileStreamWithName(stm, fn))
         {
             stm.Close();
         }
     }
 }
Пример #10
0
 private static bool RestoreFromFileNewSize(string fn)
 {
     return(SafetyFile.RestoreFromFileWithSize(fn, "_new_"));
 }
Пример #11
0
 private static bool RestoreFromFileOldSize(string fn)
 {
     return(SafetyFile.RestoreFromFileWithSize(fn, "_old_"));
 }