SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
AssociatedFile.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Xml;
6 using System.Xml.Serialization;
7 using System.IO;
8 
9 namespace Seci.Definitions
10 {
18  public class AssociatedFile
19  {
20  //The local filepath of the file
21  private String _fileLocation;
22  //The actual file data as a byte array.
23  private Byte[] _fileData;
24  //When the file was copied
25  private DateTime _lastModifiedTime;
26  //The component to which this exe belongs.
27  private String _owningComponent;
28 
29  //Properties
30  public String FileLocation { get { return _fileLocation; } set { _fileLocation = value; } }
31  public Byte[] FileData { get { return _fileData; } set { _fileData = value; } }
32  [XmlIgnore] public String OwningComponent { get { return _owningComponent; } set { _owningComponent = value; } }
33 
37  public AssociatedFile()
38  {
39  }
40 
45  public AssociatedFile(String fileName)
46  {
47  _fileLocation = fileName;
48  updateModifiedTime();
49  }
50 
55  public Boolean ConvertFileToByteArray()
56  {
57  try
58  {
59  FileInfo fInfo = new FileInfo(_fileLocation);
60 
61  Int64 numBytes = fInfo.Length;
62 
63  FileStream fStream = new FileStream(_fileLocation, FileMode.Open, FileAccess.Read);
64 
65  BinaryReader br = new BinaryReader(fStream);
66 
67  Byte[] data = br.ReadBytes((int)numBytes);
68 
69  br.Close();
70  fStream.Close();
71 
72  _fileData = data;
73 
74  updateModifiedTime();
75 
76  return true;
77  }
78  catch
79  {
80  //Sets _fileLocation and _fileData to null if an error is thrown.
81  _fileLocation = null;
82  _fileData = null;
83  return false;
84  }
85  }
86 
93  public void CreateFile()
94  {
95  if (_fileLocation != null && _fileData != null && _fileData.Length > 0)
96  {
97  //Get existing file and rename it to .bak
98  FileInfo fOld = new FileInfo(_fileLocation);
99  if (fOld.Exists)
100  {
101  fOld.CopyTo(_fileLocation + ".bak", true);
102  }
103 
104  //Create binary file from bytes
105  FileStream fStream = new FileStream(_fileLocation, FileMode.Create, FileAccess.Write);
106  BinaryWriter bw = new BinaryWriter(fStream);
107 
108  try
109  {
110  bw.Write(_fileData);
111  }
112  catch (Exception e)
113  {
114  //throw error up to next level
115  throw new ArgumentException(e.Message);
116  }
117  finally
118  {
119  bw.Close();
120  fStream.Close();
121 
122  //Also create another copy with the extension .orig
123  CreateOriginal();
124 
125  //Set _Filedata to null to free up memory
126  _fileData = null;
127  }
128 
129  updateModifiedTime();
130  }
131  }
132 
136  private void CreateOriginal()
137  {
138  try
139  {
140  FileInfo fOrig = new FileInfo(_fileLocation);
141  if (fOrig.Exists)
142  {
143  fOrig.CopyTo(_fileLocation + ".orig", true);
144  }
145  }
146  catch
147  {
148  }
149  }
150 
155  public void RestoreOriginal()
156  {
157  try
158  {
159  FileInfo fOrig = new FileInfo(_fileLocation + ".orig");
160  if (fOrig.Exists)
161  {
162  fOrig.CopyTo(_fileLocation, true);
163  }
164  }
165  catch
166  {
167  }
168  }
169 
173  private void updateModifiedTime()
174  {
175  //Get time of creation/copy
176  try
177  {
178  _lastModifiedTime = getLastModifiedDateTime();
179  }
180  catch
181  {
182  //Do nothing
183  }
184  }
185 
190  public Boolean HasBeenModified()
191  {
192  try
193  {
194  DateTime lastMod = getLastModifiedDateTime();
195  if (lastMod > _lastModifiedTime)
196  {
197  return true;
198  }
199  }
200  catch
201  {
202  //Do nothing
203  }
204 
205  return false;
206  }
207 
212  private DateTime getLastModifiedDateTime()
213  {
214  FileInfo fInfo = new FileInfo(_fileLocation);
215 
216  if (!fInfo.Exists)
217  {
218  throw new Exception("File does not exist!");
219  }
220 
221  return fInfo.LastWriteTime;
222  }
223  }
224 }
AssociatedFile(String fileName)
Recommended constructor for use in code.
void CreateOriginal()
Creates a copy of the file but with the .orig extension
void updateModifiedTime()
Method for updating the last modified time.
Boolean ConvertFileToByteArray()
Method for converting a file to a byte array.
Boolean HasBeenModified()
Method for checking whether the file was modified.
void CreateFile()
Method for creating a file from the saved byte array. Will back-up the existing version of the file a...
This class is used for storing files as part of a configuration. Typically, the files stored are ini ...
AssociatedFile()
Default constructor
DateTime getLastModifiedDateTime()
Method for getting the last time when the file was modified.
void RestoreOriginal()
Restores the on disk copy of the associated file to what was originally in the configuration. In other words, it overwrites the file with the .orig file.