OpenKeyWord  Version: 426, Datum:
OKW_FileHelper.cs
1 #region Header
2 /*
3  ==============================================================================
4  Author: Zoltan Hrabovszki <zh@openkeyword.de>
5 
6  Copyright © 2012, 2013, 2014, 2015 IT-Beratung Hrabovszki
7  www.OpenKeyWord.de
8  ==============================================================================
9 
10  This file is part of OpenKeyWord.
11 
12  OpenKeyWord is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  OpenKeyWord is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with OpenKeyWord. If not, see <http://www.gnu.org/licenses/>.
24 
25  Diese Datei ist Teil von OpenKeyWord.
26 
27  OpenKeyWord ist Freie Software: Sie können es unter den Bedingungen
28  der GNU General Public License, wie von der Free Software Foundation,
29  Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
30  veröffentlichten Version, weiterverbreiten und/oder modifizieren.
31 
32  OpenKeyWord wird in der Hoffnung, dass es nützlich sein wird, aber
33  OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
34  Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
35  Siehe die GNU General Public License für weitere Details.
36 
37  Sie sollten eine Kopie der GNU General Public License zusammen mit
38  OpenKeyWord erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
39 */
40 #endregion Header
41 
42 namespace OKW
43 {
44  using System;
45  using System.IO;
46  using System.Text;
47  using OKW.Log;
48  using OKW.ANTLR4;
49 
58  public static class OKW_FileHelper
59  {
60  #region Methods
61 
74  public static void DirectoryCreateEmpty(string fpsPath)
75  {
76  string lvsPath = string.Empty;
77  try
78  {
79  lvsPath = MyParser.ParseMe(fpsPath);
80 
81  // Determine whether the directory exists.
82  if (Directory.Exists(lvsPath))
83  {
84  DirectoryDelete(lvsPath);
85  }
86  else if (File.Exists(lvsPath))
87  {
88  FileDelete(lvsPath);
89  }
90 
91  // Try to create the directory.
92  DirectoryInfo di = Directory.CreateDirectory(lvsPath);
93  //Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
94  }
95  catch (Exception e)
96  {
97  Console.WriteLine("The process failed: {0}", e.ToString());
98  }
99  finally {}
100 
101 
102  }
115  public static void DirectoryDelete(string fpsPaFiNa)
116  {
117  string lvsPaFiNa = string.Empty;
118 
119  lvsPaFiNa = MyParser.ParseMe(lvsPaFiNa);
120 
121  if (System.IO.Directory.Exists(fpsPaFiNa))
122  {
123  // Use a try block to catch IOExceptions, to
124  // handle the case of the file already being
125  // opened by another process.
126  try
127  {
128  System.IO.Directory.Delete(lvsPaFiNa, true);
129  }
130  catch (System.IO.IOException e)
131  {
132  Console.WriteLine(e.Message);
133  return;
134  }
135  }
136 
137  return;
138  }
139 
150  public static bool DirectoryExist(string fpsPaFiNa)
151  {
152  string lvsPaFiNa = string.Empty;
153 
154  lvsPaFiNa = MyParser.ParseMe(fpsPaFiNa);
155 
156  return System.IO.Directory.Exists(lvsPaFiNa);
157  }
158 
170  public static void DirectoryMove(string fpsPaNaSource, string fpsPaNaDestination)
171  {
172 
173  string lvsPaNaSource = string.Empty;
174  string lvsPaNaDestination = string.Empty;
175 
176  lvsPaNaSource = MyParser.ParseMe(fpsPaNaSource);
177  lvsPaNaDestination = MyParser.ParseMe(fpsPaNaDestination);
178 
179  if (System.IO.Directory.Exists(lvsPaNaSource))
180  {
181  // Use a try block to catch IOExceptions, to
182  // handle the case of the file already being
183  // opened by another process.
184  try
185  {
186  // Löschen des ZIEL-verzeichnissen wenn vorhanden
187  System.IO.Directory.Delete(lvsPaNaDestination, true);
188  System.IO.Directory.Move(lvsPaNaSource, lvsPaNaDestination);
189  }
190  catch (System.IO.IOException e)
191  {
192  Console.WriteLine(e.Message);
193  return;
194  }
195  }
196 
197  return;
198  }
199 
200  public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
201  {
202  // Get the subdirectories for the specified directory.
203  var dir = new DirectoryInfo(sourceDirName);
204  DirectoryInfo[] dirs = dir.GetDirectories();
205 
206  if (!dir.Exists)
207  {
208  throw new DirectoryNotFoundException(
209  "Source directory does not exist or could not be found: "
210  + sourceDirName);
211  }
212 
213  // If the destination directory doesn't exist, create it.
214  if (!Directory.Exists(destDirName))
215  {
216  Directory.CreateDirectory(destDirName);
217  }
218 
219  // Get the files in the directory and copy them to the new location.
220  FileInfo[] files = dir.GetFiles();
221  foreach (FileInfo file in files)
222  {
223  string temppath = Path.Combine(destDirName, file.Name);
224  file.CopyTo(temppath, false);
225  }
226 
227  // If copying subdirectories, copy them and their contents to new location.
228  if (copySubDirs)
229  {
230  foreach (DirectoryInfo subdir in dirs)
231  {
232  string temppath = Path.Combine(destDirName, subdir.Name);
233  DirectoryCopy(subdir.FullName, temppath, copySubDirs);
234  }
235  }
236  }
237 
264  public static void FilesDelete(string fpsPaFiNa)
265  {
266  string lvsDirectory = Path.GetDirectoryName(fpsPaFiNa);
267  string lvsFilename = Path.GetFileName(fpsPaFiNa);
268 
269  var MyFiles = Directory.GetFiles(lvsDirectory, lvsFilename);
270 
271  foreach (var MyFile in MyFiles)
272  {
273  if (File.Exists(MyFile))
274  {
275  File.Delete(MyFile);
276  }
277  }
278  return;
279  }
280 
295  public static void FileDelete(string fpsPaFiNa)
296  {
297  if (System.IO.File.Exists(fpsPaFiNa))
298  {
299  // Use a try block to catch IOExceptions, to
300  // handle the case of the file already being
301  // opened by another process.
302  try
303  {
304  System.IO.File.Delete(fpsPaFiNa);
305  }
306  catch (System.IO.IOException e)
307  {
308  Console.WriteLine(e.Message);
309  return;
310  }
311  }
312 
313  return;
314  }
315 
326  public static bool FileExist(string fpsPaFiNa)
327  {
328  bool lvbReturn = false;
329  Logger.Instance.LogFunctionStartDebug("OKW_FileHelper.FileExist", "fpsPaFiNa", fpsPaFiNa);
330 
331  try
332  {
333  if (Directory.Exists(fpsPaFiNa))
334  {
335  throw(new OKWFileDoesNotExistsException("This is not a File! Given path is a directory!"));
336  }
337  else
338  {
339  lvbReturn = File.Exists(fpsPaFiNa);
340  }
341  }
342  finally
343  {
344  Logger.Instance.LogFunctionEndDebug(lvbReturn.ToString());
345  }
346  return lvbReturn;
347  }
348 
360  public static void FileMove(string fpsPaFiNaSource, string fpsPaFiNaDestination)
361  {
362  if (System.IO.File.Exists(fpsPaFiNaSource))
363  {
364  // Use a try block to catch IOExceptions, to
365  // handle the case of the file already being
366  // opened by another process.
367  try
368  {
369  System.IO.File.Delete(fpsPaFiNaDestination);
370  System.IO.File.Move(fpsPaFiNaSource, fpsPaFiNaDestination);
371  }
372  catch (System.IO.IOException e)
373  {
374  Console.WriteLine(e.Message);
375  return;
376  }
377  }
378  else
379  {
380  // Exception auslösen: Datei di nicht vorhanden is kann nicht verschoben werden.
381  }
382 
383  return;
384  }
385 
396  public static void FileCreate(string fpsPaFiNa)
397  {
398  FileStream myFileStream = File.Open(fpsPaFiNa, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
399  myFileStream.Close();
400  myFileStream.Dispose();
401  File.SetLastWriteTimeUtc(fpsPaFiNa, DateTime.UtcNow);
402  }
403 
413  public static bool IsFolderEmpty(string Folder)
414  {
415  try
416  {
417  if (Directory.GetDirectories(Folder).Length == 0)
418  {
419  if (Directory.GetFiles(Folder).Length == 0)
420  {
421  return true;
422  }
423  }
424  }
425  catch (Exception)
426  {
427  return false;
428  }
429 
430  return false;
431  }
432 
444  private static void CopyDirectoryWithIncludedFiles(string dirCopySource, string dirCopyTarget)
445  {
446  // alle Unterverzeichnisse des aktuellen Verzeichnisses ermitteln
447  string[] subDirectories = System.IO.Directory.GetDirectories(dirCopySource);
448 
449  // Zielpfad erzeugen
450  StringBuilder newTargetPath = new StringBuilder();
451  {
452  newTargetPath.Append(dirCopyTarget);
453  newTargetPath.Append(dirCopySource.Substring(dirCopySource.LastIndexOf(@"\")));
454  }
455 
456  // wenn aktueller Ordner nicht existiert -> ersstellen
457  if (!Directory.Exists(newTargetPath.ToString()))
458  {
459  Directory.CreateDirectory(newTargetPath.ToString());
460  }
461 
462  // Unterverzeichnise durchlaufen und Funktion mit dazu gehörigen Zielpfad erneut aufrufen(Rekursion)
463  foreach (string subDirectory in subDirectories)
464  {
465  string newDirectoryPath = subDirectory;
466 
467  // wenn ''/'' an letzter Stelle dann entfernen
468  if (newDirectoryPath.LastIndexOf(@"\") == (newDirectoryPath.Length - 1))
469  {
470  newDirectoryPath = newDirectoryPath.Substring(0, newDirectoryPath.Length - 1);
471  }
472 
473  // rekursiever Aufruf
474  CopyDirectoryWithIncludedFiles(newDirectoryPath, newTargetPath.ToString());
475  }
476 
477  // alle enthaltenden Dateien des aktuellen Verzeichnisses ermitteln
478  string[] fileNames = Directory.GetFiles(dirCopySource);
479  foreach (string fileSource in fileNames)
480  {
481  // Zielpfad + Dateiname
482  StringBuilder fileTarget = new StringBuilder();
483  {
484  fileTarget.Append(newTargetPath);
485  fileTarget.Append(fileSource.Substring(fileSource.LastIndexOf(@"\")));
486  }
487 
488  // Datei kopieren, wenn schon vorhanden überschreiben
489  File.Copy(fileSource, fileTarget.ToString(), true);
490  }
491  }
492 
507  public static string ConvertDirectorySeperator(string fpsPath)
508  {
509  string lvsReturn = fpsPath;
510 
511  if ( System.IO.Path.DirectorySeparatorChar.ToString() == @"/" )
512  {
513  lvsReturn = fpsPath.Replace(@"\", @"/");
514  }
515  else if (System.IO.Path.DirectorySeparatorChar.ToString() == @"\")
516  {
517  lvsReturn = fpsPath.Replace(@"/", @"\");
518  }
519 
520  return lvsReturn;
521  }
522 
523 
524 
525  #endregion Methods
526  }
527 }
static bool IsFolderEmpty(string Folder)
Diese Funktion prüft, ob der angegebene Ordner leer ist.
static void FileDelete(string fpsPaFiNa)
Löscht die gegebene Datei fpsPaFiNa.
static void FileMove(string fpsPaFiNaSource, string fpsPaFiNaDestination)
Verschiebt die gegeben Quell-Datei zu einer neuen Ziel-Datei.
static string ConvertDirectorySeperator(string fpsPath)
Konvertiert selbständig.
static void FilesDelete(string fpsPaFiNa)
Löscht eine oder mehrere Dateien.
static void DirectoryCreateEmpty(string fpsPath)
Erzeug ein leeres Verzechniss mit dem Gegebene nmaen.
static void DirectoryDelete(string fpsPaFiNa)
Recursives löschen des gegebenen Verzeichnisses.
static bool DirectoryExist(string fpsPaFiNa)
Prüft, ob die gegebene fpsPaFiNa Datei existiert.
static void FileCreate(string fpsPaFiNa)
Legt eine Leere Datei an.
Description of OKW_FileHelper.
static bool FileExist(string fpsPaFiNa)
Prüft, ob die gegebene fpsPaFiNa Datei existiert.
static List< string > ParseMe(List< string > fpLsString2Parse)
Parst einen _List< string>, ersetzt die Parser-Schlüsslewörter durch Werte.
Definition: Parser.cs:31
static void CopyDirectoryWithIncludedFiles(string dirCopySource, string dirCopyTarget)
Kopiert das angegebene Quellverzeichnis mit den beinhaltenden Verzeichnissen und Dateien in das angeg...
static void DirectoryMove(string fpsPaNaSource, string fpsPaNaDestination)
Verzeichniss verschieben löschen des gegebenen Verzeichnisses..
Definition: Core.cs:40