示例#1
0
 public FileManagerResult GetFileIcon(string filePath)
 {
     result = new FileManagerResult();
     try
     {
         if (icon == null && System.IO.File.Exists(filePath))
         {
             using (System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(filePath))
             {
                 icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                     sysicon.Handle,
                     System.Windows.Int32Rect.Empty,
                     System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
             }
             result.ImageSourceObj = icon;
             return(result);
         }
         else
         {
             result.ImageSourceObj = null;
             result.Errors.Add(string.Empty);
             return(result);
         }
     }
     catch (Exception e)
     {
         result.Exceptions.Add(e);
     }
     return(result);
 }
示例#2
0
 public FileManagerResult SaveText(string filePath, string content, Encoding encoding)
 {
     result = new FileManagerResult();
     try
     {
         File.WriteAllText(filePath, content, encoding);
         result.IsFileWritten = true;
         return(result);
     }
     catch (Exception e)
     {
         result.Exceptions.Add(e);
         return(result);
     }
 }
示例#3
0
 public FileManagerResult GetFileThumbnailImage(string filePath, int width, int height)
 {
     result = new FileManagerResult();
     try
     {
         Image thumbnail = ShellFile.FromFilePath(filePath).Thumbnail.Bitmap.GetThumbnailImage(width, height, () => false, IntPtr.Zero);
         result.ImageSourceObj = BitmapToImageSource(new Bitmap(thumbnail, width, height));
         return(result);
     }
     catch (Exception e)
     {
         result.Exceptions.Add(e);
         return(result);
     }
 }
示例#4
0
 public FileManagerResult GetFileStream(string filePath, FileMode fileMode, FileAccess fileAccess)
 {
     result = new FileManagerResult();
     try
     {
         if (File.Exists(filePath))
         {
             result.FileStreamObj = new FileStream(filePath, fileMode, fileAccess);
         }
         else
         {
             result.Errors.Add(FileManagerServiceConstants.FilePathDoesNotExist);
         }
         return(result);
     }
     catch (Exception e)
     {
         result.Exceptions.Add(e);
         return(result);
     }
 }
 private void SaveEncryptionToFile(string filePath, string content, Encoding encoding)
 {
     try
     {
         using (FileManager fileManager = new FileManager())
         {
             FileManagerResult fmr = fileManager.SaveText(filePath, content, encoding);
             if (fmr.IsFileWritten)
             {
                 result.EncryptedToFilePath = filePath;
                 result.IsEncryptedToFile   = true;
             }
         };
     }
     catch (Exception e)
     {
         result.IsEncryptedToFile = false;
         result.Exceptions.Add(e);
         WriteToConsole(EncryptionManagerConstants.EncryptionToFileFailure + e.Message);
     }
 }
 public EncryptionResult Encrypt(EncryptionRequest encryptionRequest)
 {
     result = new EncryptionResult();
     try
     {
         using (FileManager fileManager = new FileManager())
         {
             FileManagerResult fileManagerResult = fileManager.GetStreamReaderText(encryptionRequest.OriginalFilePath, encryptionRequest.TextEncoding);
             if (fileManagerResult.IsFileStreamTextValid)
             {
                 if (string.IsNullOrEmpty(_key))
                 {
                     _key = EncryptionConfig.DefaultEncryptionKey;
                 }
                 result.EncryptedText       = Encrypt(fileManagerResult.FileStreamText, _key);
                 result.EncryptedToFilePath = encryptionRequest.EncryptToFilePath;
                 if (!string.IsNullOrEmpty(encryptionRequest.EncryptToFilePath))
                 {
                     SaveEncryptionToFile(encryptionRequest.EncryptToFilePath, result.EncryptedText, encryptionRequest.TextEncoding);
                     result.IsEncrypted = true;
                 }
             }
             else
             {
                 result.IsEncrypted = false;
                 result.Errors.Add(EncryptionManagerConstants.InvalidFileStream);
                 WriteToConsole(EncryptionManagerConstants.InvalidFileStream);
             }
         }
         return(result);
     }
     catch (Exception e)
     {
         result.IsEncrypted = false;
         result.Exceptions.Add(e);
         WriteToConsole(EncryptionManagerConstants.InvalidInput + e.Message);
         return(result);
     }
 }
 /// <summary>
 /// Deserialize the given deserialization request object to an object of the sepcified
 /// </summary>
 /// <param name="serializationRequest"></param>
 /// <returns></returns>
 public SerializationResult DeserializeToObject(DeserializationRequest deserializationRequest)
 {
     result = new SerializationResult();
     result.IsDeserialized = false;
     try
     {
         XmlSerializer serializer = new XmlSerializer(deserializationRequest.DeserializationType);
         if (File.Exists(deserializationRequest.InputFilePath))
         {
             using (FileManager fileManager = new FileManager())
             {
                 FileManagerResult fileManagerResult = fileManager.GetFileStream(deserializationRequest.InputFilePath);
                 if ((fileManagerResult != null) && (fileManagerResult.IsFileStreamValid))
                 {
                     using (fileManagerResult.FileStreamObj)
                     {
                         result.DeserializedObject = serializer.Deserialize(fileManagerResult.FileStreamObj);
                         result.IsDeserialized     = true;
                     }
                 }
                 else
                 {
                     result.Errors.Add(FileManagerServiceConstants.InvalidFileStreamObject);
                 }
             }
         }
         else
         {
             result.Errors.Add(FileManagerServiceConstants.FilePathDoesNotExist);
         }
         return(result);
     }
     catch (Exception e)
     {
         result.Exceptions.Add(e);
         return(result);
     }
 }
示例#8
0
 public FileManagerResult GetStreamReaderText(string filePath, Encoding encoding)
 {
     result = new FileManagerResult();
     try
     {
         if (File.Exists(filePath))
         {
             using (var streamReader = new StreamReader(filePath, encoding))
             {
                 result.FileStreamText = streamReader.ReadToEnd();
             }
         }
         else
         {
             result.Errors.Add(FileManagerServiceConstants.FilePathDoesNotExist);
         }
         return(result);
     }
     catch (Exception e)
     {
         result.Exceptions.Add(e);
         return(result);
     }
 }