Laden...

Auto ArchivDirectory

Letzter Beitrag vor 13 Jahren 5 Posts 5.334 Views
Auto ArchivDirectory

Beschreibung:

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

Hallo mosspower,

nette komponente aber könntest du evtl dein exceptionhandling überarbeiten? es ist nicht gut origianale exceptions in etwas kaum aussagefähiges zu übersetzen.

gruß
Jack

Ich habe jetzt noch angepasst, dass jährliche, monatliche und tägliche Ordner erstellt werden. Die ursprünglichen Exceptions habe ich in InnerExceptions verpackt - könnt ihr ja anpassen, wenn nicht gewünscht.

     
    /// <summary>
    /// Gets the full qualified archive path and creates all neccessary sub 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">If qArchiveRoot is empty or null, all other
    /// Exceptions will be wrapped in inner 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 annually directory
        qProcessingDirName = Path.Combine(processingRootInfo.FullName,
          processingDateTime.ToString("yyyy"));

        if(!Directory.Exists(qProcessingDirName)) {
          try {
            Directory.CreateDirectory(qProcessingDirName);
          }
          catch(Exception ex) {
            throw new ArgumentException(String.Format("Cannot create annually " +
              "archive folder '{0}' because of '{1}'",
                new Object[] { qProcessingDirName, ex.Message }), ex);
          }
        }

        // Handle monthly directory
        qProcessingDirName = Path.Combine(qProcessingDirName,
          processingDateTime.ToString("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 }), ex);
          }
        }

        // Handly daily directory
        if(dailyArchive) {
          qProcessingDirName = Path.Combine(qProcessingDirName,
            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 }), ex);
            }
          }
        }

        archiveDir = qProcessingDirName;
      }
      catch {
        throw;
      }

      return archiveDir;
    }
IOUtil

Hallo,

ich kann den Namespace IOUtil nicht finden?

Kann mir jemand helfen?

Besten Dank
dimuwe

So hat mosspower einfach seine Klasse genannt (auch wenn er den Klassennamen nicht gepostet hat), d.h. nenne die Klasse auch so oder ersetze 'IOUtil' durch deinen Klassennamen.
Alternativ kannst du auch einfach "IOUtil." löschen, da ja einfach eine andere Überladung der Methode derselben Klasse aufgerufen wird.