0

ファイルはEDI経由で.todayとして取り込まれます。 .txtに変更して別のフォルダに移動する必要があります。すべてが3〜5ファイルほどうまく動作し、例外をスローします。私はそれらを処理しようとしましたが、それは問題を解決しません。私はまた散発的(ファイル名はnullにすることはできません)例外も同様になっています。私はこれを理解していないようです。FileSystemWatcherは例外をスローする前に3-5回動作します。 - C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Diagnostics; 
using System.Threading; 
using System.Windows.Forms; 
using System.Security.Permissions; 

namespace FileConverterService 
{ 


    class ConverterService 
    { 



     //Configure watcher & input 
     private FileSystemWatcher _watcher; 

     public bool Start() 
     { 
      _watcher = new FileSystemWatcher(@"C:\FTP_base\temp", "*.today"); 

      _watcher.Created += new FileSystemEventHandler(FileCreated); 

      _watcher.IncludeSubdirectories = false; 

      _watcher.EnableRaisingEvents = true; 

      return true; 
     } 




     //Configure output creation and append file name to include .txt extension 
     [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] 
     private void FileCreated(object sender, FileSystemEventArgs e) 
     { 
      try 
      { 
       string dateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff"); 
       string content = File.ReadAllText(e.FullPath); 
       string upperContent = content.ToUpperInvariant(); 
       var dir = Path.GetDirectoryName(e.FullPath); 
       var convertedFileName = Path.GetFileName(e.FullPath) + dateTime + ".txt"; 
       var convertedPath = Path.Combine(dir, convertedFileName); 

       File.WriteAllText(convertedPath, upperContent); 


      } 
      catch (IOException f) 
      { 
       if (f is IOException) 
       { 
        MessageBox.Show("Exception Caught"); //was just testing 
       } 
      } 

      MoveConvert(); 
     } 

     //Move converted file to EDI processing folder 
     public static void MoveConvert() 
     { 
      try { 
      string dateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff"); 
      string rootFolderPath = @"C:\FTP_base\temp\"; 
      string moveTo = @"C:\FTP_base\INbound\inbound_" + dateTime + ".txt"; 
      //string moveTo = @"F:\FTP_base\Office Depot\INbound\inbound_" + dateTime + ".txt"; 
      string filesToMove = @"*.txt"; // Only move .txt 

      string myfile2 = System.IO.Directory.GetFiles(rootFolderPath, filesToMove).FirstOrDefault(); 
      string fileToMove = myfile2; 

      //moving file 
      File.Move(fileToMove, moveTo); 


       MoveOriginal(); 

      } 
      catch (IOException e) 
      { 
       if (e is IOException) 
       { 
        MessageBox.Show("File already exists."); //was just testing 
       } 
      } 
     } 


     public static void MoveOriginal() 
     { 
      try { 
      string dateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff"); 
      string rootFolderPath2 = @"C:\FTP_base\temp\"; 
      string moveTo2 = @"C:\FTP_base\archive\archive_" + dateTime + ".archive"; 
      //string moveTo2 = @"F:\Xcelerator_EDI\OfficeDepot\DataFiles\Inbound\Archive2\archive_" + dateTime + ".archive"; 
      string filesToMove2 = @"*.today"; // Only move .today 


      string myfile = System.IO.Directory.GetFiles(rootFolderPath2, filesToMove2).FirstOrDefault(); 
      //foreach (string file in fileList) 

      string fileToMove2 = myfile; 

      //moving file 
      File.Move(fileToMove2, moveTo2); 

      } 
      catch (IOException e) 
      { 
       if (e is IOException) 
       { 
        MessageBox.Show("IO Exception Occurred"); //was just testing 
       } 
      } 
     } 



     //Stop Service control 
     public bool Stop() 
     { 
      _watcher.Dispose(); 

      return true; 
     } 
    } 
} 
+0

例外をスローする前に一度しか動作しないことがあります。スローする前に行内に5,6があることがあります。非常に奇妙な。 – Derek

+0

どの行でそれが起こりますか? –

+0

'FileSystemWatcher'は、多くのエラー状態を持っているので、使いにくいことはよく知られています。 Rxを使用することができて、別のフルフィルメント会社によって作成されたコードを気にしない場合は、[ラッパー](https://idcomlog.codeplex.com/SourceControl/latest#IdComLog.Reactive/FileSystem.cs)( [NuGet](https://www.nuget.org/packages/IdComLog.Reactive/)でも私はこれをより信頼性の高いものにするために作成しました。 –

答えて

0

ファイルがまだ使用中である可能性があります。 FileSystemWatcherイベントは、ファイルが作成されたときに発生しますが、作成プロセスはまだファイルに書き込むことができます。考えられる解決策については、hereを参照してください。

+0

ありがとうございました!私は研究を続けます – Derek

関連する問題