123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Drawing;
- using System.IO;
- namespace Aio1Ef.Packer.Common
- {
- internal class TempFile : IDisposable
- {
- private readonly string _filePath;
- private readonly bool _containsFileName;
- public string FilePath
- {
- get { return _filePath; }
- }
- public TempFile(byte[] data, string fileName = null)
- {
- if (data == null)
- {
- IsValid = false;
- return;
- }
- _filePath = Path.GetTempFileName();
- if (fileName != null)
- {
- File.Delete(_filePath);
- Directory.CreateDirectory(_filePath);
- _filePath = Path.Combine(_filePath, fileName);
- _containsFileName = true;
- }
- File.WriteAllBytes(_filePath, data);
- IsValid = true;
- }
- public bool IsValid { get; private set; }
- private static byte[] Icon2Bytes(Icon icon)
- {
- if (icon == null)
- return null;
- using (var ms = new MemoryStream())
- {
- icon.Save(ms);
- return ms.ToArray();
- }
- }
- public TempFile(Icon icon, string fileName = null)
- : this(Icon2Bytes(icon), fileName)
- {
- }
- public void Dispose()
- {
- if (IsValid == false)
- return;
- if (_containsFileName)
- Directory.Delete(Path.GetDirectoryName(_filePath), true);
- else
- File.Delete(_filePath);
- }
- }
- }
|