2013-06-06 2 views
1

IIS에서 프로세스를 실행하여 파일을 이동하고 보관 한 다음 일부 빈 * .cnf 파일을 생성하여 다른 응용 프로그램에서이 파일을 처리 할 수 ​​있음을 알립니다. 어쨌든, IIS는 * .cnf 파일을 공개하지 않습니다. 아래 코드를 붙여 넣었습니다. 바라건대 누군가가 내 코드에 뭔가 있거나 IIS 라고만 지적하면 도움이 될 것입니다.IIS가 파일을 만든 후 파일을 해제하지 않음

public void Run() 
     { 
      try 
      { 
       // Log start of job 
       _log.Info("GL file copy job started ..."); 

       // Check if today's date is not a holiday before processing these GL files 
       if (!_prov.IsGlHoliday()) 
       { 
        // Get tmp directory 
        string glTempDirPath = GLConstants.Directories.GLTmp; 

        // Copy files from tmp to production drop off directory 
        if (Directory.Exists(glTempDirPath)) 
        { 
         // Get tmp directory information 
         DirectoryInfo glTempDirInfo = new DirectoryInfo(glTempDirPath); 

         // Get all files at directory root level only 
         FileInfo[] glTempFiles = glTempDirInfo.GetFiles("*", SearchOption.TopDirectoryOnly); 

         // Check if any error files were generated during validation process 
         bool errorsFound = false; 
         foreach (FileInfo f in glTempFiles) 
         { 
          if (!f.Attributes.HasFlag(FileAttributes.Hidden) && f.Name.ToLower().IndexOf("error") != -1) 
          { 
           errorsFound = true; 
           break; 
          } 
         } 

         if (!errorsFound) 
         { 
          // Get GL data and confirmation files 
          List<FileInfo> glFiles = new List<FileInfo>(); 
          foreach (FileInfo f in glTempFiles) 
          { 
           if (!f.Attributes.HasFlag(FileAttributes.Hidden) && f.Name.ToLower().IndexOf("error") == -1) 
           { 
            glFiles.Add(f); 
           } 
          } 
          _log.Info(string.Format("Copy and archiving {0} GL file{1} ...", glFiles.Count.ToString(), (glFiles.Count > 0) ? "s" : string.Empty)); 

          // Archive Data files only 
          // 1. Create archive directory if it does not exist 
          // 2. Move files from temporary directory to archive directory 
          string glArchiveDirPath = Path.Combine(GLConstants.Directories.GLArchive, DateTime.Now.ToString("MMddyyyy")); 

          DirectoryInfo glArchiveDirInfo = new DirectoryInfo(glArchiveDirPath); 
          if (!(glArchiveDirInfo.Exists)) { glArchiveDirInfo.Create(); } 

          foreach (FileInfo f in glFiles) 
          { 
           string srcDestination = f.FullName; 
           string archiveDestination = Path.Combine(glArchiveDirPath, f.Name); 
           f.CopyTo(archiveDestination, true); 
           _log.Info(string.Format("GL file archived from {0} to {1} ...", srcDestination, f.FullName)); 
          } 

          // (1) Move data files 
          foreach (FileInfo f in glFiles) 
          { 
           string moveToDestination = Path.Combine(GLConstants.Directories.GL, f.Name); 
           f.MoveTo(moveToDestination); 
           _log.Info(string.Format("GL file moved from {0} to {1} ...", f.FullName, moveToDestination)); 
          } 

          // (2) Create confirmation files (.cnf) for these data files (TIF, BIOS, INPAXRF) 
          List<string> cnfFiles = new List<string>(); 
          foreach (FileInfo f in glFiles) 
          { 
           if (
            (f.Name.IndexOf("TIF", StringComparison.OrdinalIgnoreCase) != -1) 
            || 
            (f.Name.IndexOf("BIOS", StringComparison.OrdinalIgnoreCase) != -1) 
            || 
            (f.Name.IndexOf("INPAXRF", StringComparison.OrdinalIgnoreCase) != -1) 
            ) 
           { 
            cnfFiles.Add(f.Name); 
           } 
          } 

          foreach (string f in cnfFiles) 
          { 
           string cnfFile = Path.Combine(GLConstants.Directories.GL, string.Format("{0}{1}", f, ".cnf")); 
           File.CreateText(cnfFile); 
          }                     

          NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy", msg = "GL file copy process completed." }); 
         } 
         else 
         { 
          _log.Info("One or more error files detected, GL file copy process terminated ..."); 
          NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy", msg = "One or more error files detected, GL file copy process terminated." }); 
         } 
        } 
        else 
        { 
         _log.Info(string.Format("{0} path does not exist, GL file copy process terminated ...", glTempDirPath)); 
         NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy", msg = string.Format("{0} path does not exist, GL file copy process terminated.", glTempDirPath) }); 
        } 
       } 
       else 
       { 
        _log.Info(string.Format("{0} is a holiday, GL file copy process terminated ...", DateTime.Now.ToString("d"))); 
        NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy", msg = string.Format("{0} is a holiday, GL file copy process terminated.", DateTime.Now.ToString("d")) }); 
       } 

       // Log end of job 
       _log.Info("GL file copy job ended ..."); 
      } 
      catch (Exception ex) 
      { 
       _log.Fatal(string.Format("{0} : {1} ({2})", ex.Source, ex.Message, ex.ToString())); 
       NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy (ERROR)", msg = ex.Message }); 
      } 
     } 

답변

0

나는 그것이 파일을 생성하고 당신을 위해 StreamWriter을 반환하기 전에 때문에 File.* 방법과 문제를 했어하지만 당신은 GC가 해제 파일을 가지고 그것을 폐기 주위 때까지 기다려야 할 것입니다.

private static void CreateFile(string fileName) 
{ 
    FileStream file = new FileStream(fileName,FileMode.Create,FileAccess.Write,FileShare.None); 
    file.Close(); 
} 

이 또한 그것에 대해 생각 작업은 (그것을 시도하지) 수 :

대신 Close() 호출 수동으로 사용자 스스로 FileStream를 사용에 File.CreateText()을 변경하려고 문제가 있었다

File.CreateText(...).Close();

+0

. File.CreateText (...)는 스트림을 열어 두었습니다. 방금 File.CreateText (...)를 추가했습니다. Close()와 모두 만족합니다. 다시 한번 감사드립니다. – VajNyiaj

관련 문제