WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
FileInfoExtensions.cs
Go to the documentation of this file.
1 using System.IO;
2 using System.Security.Cryptography;
3 using System.Linq;
4 using System;
5 
6 
10 public static class FileInfoExtensions
11 {
17  public static string GetNameWithoutExtension(this FileInfo f)
18  {
19  if (f == null) return "";
20  if (!f.Extension.IsNullOrWhiteSpace())
21  {
22  return f.Name.Left(f.Name.Length - f.Extension.Length);
23  };
24  return f.Name;
25  }
26 
27 
28 
33  public static string ReadFileToString(this FileInfo f)
34  {
35  StreamReader streamReader = f.OpenText();
36  string Data = streamReader.ReadToEnd();
37  streamReader.Close();
38  streamReader.Dispose();
39  return Data;
40  }
41 
46  public static bool CheckExists(this FileInfo FI)
47  {
48  return new FileInfo(FI.FullName).Exists;
49  }
50 
56  public static string GetMD5Hash(this FileInfo FI)
57  {
58  byte[] hashBytes = null;
59  try
60  {
61  using (var inputFileStream = FI.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
62  {
63  MD5 md5 = System.Security.Cryptography.MD5.Create();
64  hashBytes = md5.ComputeHash(inputFileStream);
65  }
66  return string.Join("", hashBytes.Select(B => "{0:x2}".Build(B)).ToArray());
67 
68  }
69  catch (Exception E)
70  {
71 
72  throw new Exception("Cant calculate MD5 has of file {0}.".Build(FI.FullName), E);
73  }
74  }
75 
76 
82  public static T XMLDeserialize<T>(this FileInfo FI)
83  {
84  using (FileStream ms = FI.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
85  {
86  try
87  {
88  return (T)new System.Xml.Serialization.XmlSerializer(typeof(T)).Deserialize(ms);
89  }
90  catch (Exception E)
91  {
92 
93  Exception Ex = new Exception("Could not deserialize {0} from XML data in file: {1}".Build(typeof(T).Name, FI.FullName), E);
94  throw Ex;
95  }
96  }
97 
98  }
99 
107  public static bool CheckFileLocked(this FileInfo file)
108  {
109  FileStream stream = null;
110 
111  try
112  {
113  stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
114  }
115  catch (IOException)
116  {
117  if (!file.CheckExists())
118  {
119  throw new FileNotFoundException("File {0} not found".Build(file.FullName), file.FullName);
120  }
121  //the file is unavailable because it is:
122  //still being written to
123  //or being processed by another thread
124  //or does not exist (has already been processed)
125  return true;
126  }
127  finally
128  {
129  if (stream != null)
130  stream.Close();
131  }
132 
133  //file is not locked
134  return false;
135  }
136 
137 
138  public static bool CheckCanRead(this FileInfo file)
139  {
140  FileStream stream;
141  try
142  {
143  // try to open the file to check if we can access it for read
144  stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read);
145  stream.Dispose();
146  }
147  catch (Exception ex)
148  {
149  return false;
150  }
151  return true;
152  }
158  public static void MoveTo(this FileInfo Source, FileInfo Dest)
159  {
160  Source.MoveTo(Dest.FullName);
161  }
162 
168  public static void CopyTo(this FileInfo Source, FileInfo Dest)
169  {
170  Source.CopyTo(Dest.FullName);
171  }
172 }
173 
174