Wer Dateien archivieren möchte, muss ja monatliche oder sogar tägliche Ordner in den monatlichen anlegen. Diese Routine habe ich in die folgende Methode gekapselt. Es wird für das gegenwärtige Datum das Rootdirectory, das monatliche Directory (ISO 8601-Format) und (wenn gesetzt) das tägliche Directory erstellt (wenn jeweils nicht vorhande). Zurück gibt die Methode dann den voll-qualifizierten Archiv-Directory-Namen
/// <summary>
/// <see cref="IOUtil.GetArchiveDir(String, bool, bool)"/>
/// </summary>
/// <param name="qArchiveRoot">
/// <see cref="IOUtil.GetArchiveDir(String, bool, bool)"/>
/// </param>
/// <returns>
/// <see cref="IOUtil.GetArchiveDir(String, bool, bool)"/>
/// </returns>
public static String GetArchiveDir(String qArchiveRoot) {
return IOUtil.GetArchiveDir(qArchiveRoot, false, false);
}
/// <summary>
/// <see cref="IOUtil.GetArchiveDir(String, bool, bool)"/>
/// </summary>
/// <param name="qArchiveRoot">
/// <see cref="IOUtil.GetArchiveDir(String, bool, bool)"/>
/// </param>
/// <param name="dailyArchive">
/// <see cref="IOUtil.GetArchiveDir(String, bool, bool)"/>
/// </param>
/// <returns>
/// <see cref="IOUtil.GetArchiveDir(String, bool, bool)"/>
/// </returns>
public static String GetArchiveDir(String qArchiveRoot, bool dailyArchive) {
return IOUtil.GetArchiveDir(qArchiveRoot, false, dailyArchive);
}
/// <summary>
/// Gets the full qualified archive path and creates all neccessary folders
/// </summary>
/// <param name="qArchiveRoot">Thr root archive path</param>
/// <param name="utc">Whether convert current DateTime to UTC format</param>
/// <param name="dailyArchive">Whether daily archiving is activated or not</param>
/// <returns>The full qualified archive path</returns>
/// <exception cref="ArgumentException">Handles all possible Exceptions</exception>
public static String GetArchiveDir(String qArchiveRoot, bool utc, bool dailyArchive) {
String archiveDir = String.Empty;
DirectoryInfo processingRootInfo = null;
DateTime processingDateTime = DateTime.Now;
String qProcessingDirName = String.Empty;
try {
if(ValueUtil.IsEmptyOrDefault(qArchiveRoot)) {
throw new ArgumentException("Parameter qArchiveRoot was passed null or empty");
}
// Check whether archive root exists
if(!Directory.Exists(qArchiveRoot)) {
try {
// Create archive root directory
processingRootInfo = Directory.CreateDirectory(qArchiveRoot);
}
catch(Exception ex) {
throw new ArgumentException(String.Format("Cannot create archiv " +
"root '{0}' folder, because of '{1}'",
new Object[] { qArchiveRoot, ex.Message }));
}
}
else {
processingRootInfo = new DirectoryInfo(qArchiveRoot);
}
// Check UTC
if(utc) {
processingDateTime = processingDateTime.ToUniversalTime();
}
// Handle monthly directory
qProcessingDirName = Path.Combine(processingRootInfo.FullName,
processingDateTime.ToString("yyyy-MM"));
// Create monthly archive directory, if not exists
if(!Directory.Exists(qProcessingDirName)) {
try {
Directory.CreateDirectory(qProcessingDirName);
}
catch(Exception ex) {
throw new ArgumentException(String.Format("Cannot create monthly " +
"archive folder '{0}' because of '{1}'",
new Object[] { qProcessingDirName, ex.Message }));
}
}
// Handly daily directory
if(dailyArchive) {
qProcessingDirName = Path.Combine(processingRootInfo.FullName,
Path.Combine(processingDateTime.ToString("yyyy-MM"),
processingDateTime.ToString("dd")));
// Create daily archive directory, if not exists
if(!Directory.Exists(qProcessingDirName)) {
try {
Directory.CreateDirectory(qProcessingDirName);
}
catch(Exception ex) {
throw new ArgumentException(String.Format("Cannot create daily " +
"archive folder '{0}' because of '{1}'",
new Object[] { qProcessingDirName, ex.Message }));
}
}
}
archiveDir = qProcessingDirName;
}
catch {
throw;
}
return archiveDir;
}
Schlagwörter: Archiv, Archivierung, Autoarchive