2016-09-20 3 views
1

現在、リモートサーバーから.csvファイルを取得してローカルサーバーに同期する解析アプリケーションを作成しています。同期後、ローカルパスからダウンロードされたファイルは解析され、SQLデータベースに挿入されます。リモートサーバーから新しいファイルが追加された場合(ローカルサーバーに同期されている場合)、アプリケーションは特定の新しいファイルを解析するだけで、古い.csvファイルを再解析して再挿入することはできませんすでに解析済み)?新たに同期化されたファイル(WinSCP)を認識します

私のコードは、これまで:ファイルを同期するときに

public static int Main() 
{ 
    try 
    { 
     // Setup session options 
     SessionOptions sessionOptions = new SessionOptions 
     { 
      Protocol = Protocol.Scp, 
      HostName = hostName, 
      UserName = userName, 
      Password = passWord, 
      SshHostKeyFingerprint = sshHostKey 
     }; 

     using (Session session = new Session()) 
     { 
      // Will continuously report progress of synchronization 
      session.FileTransferred += FileTransferred; 

      // Connect 
      session.Open(sessionOptions); 

      // Synchronize files 
      SynchronizationResult synchronizationResult; 
      synchronizationResult = 
       session.SynchronizeDirectories(
        SynchronizationMode.Local, localPath, remotePath, false); 

      // Throw on any error 
      synchronizationResult.Check(); 

      Run(); 
     } 
     return 0; 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("Error: {0}", e); 
     Console.ReadLine(); 
     return 1; 
    } 
} 

これは、イベントを処理します。

private static void FileTransferred(object sender, TransferEventArgs e) 
{ 
    if (e.Error == null) 
    { 
     Console.WriteLine("Upload of {0} from remote to local server succeeded", e.FileName); 
    } 
    else 
    { 
     Console.WriteLine("Upload of {0} from remote to local server failed: {1}", e.FileName, e.Error); 
    } 

    if (e.Chmod != null) 
    { 
     if (e.Chmod.Error == null) 
     { 
      Console.WriteLine("Permisions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions); 
     } 
     else 
     { 
      Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error); 
     } 
    } 
    else 
    { 
     Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination); 
    } 

    if (e.Touch != null) 
    { 
     if (e.Touch.Error == null) 
     { 
      Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime); 
     } 
     else 
     { 
      Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error); 
     } 
    } 
    else 
    { 
     // This should never happen during "local to remote" synchronization 
     Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination); 
    } 
} 

これは.csvファイルの内容を解析します。同期後に発生します。

public static void Run() 
{ 
    dataTable(); 

    List<string> items = new List<string>(); 

    foreach (string file in Directory.EnumerateFiles(localPath, "*.csv")) 
    { 
     if (file.Contains("test")) 
     { } 
     else 
     { 
      using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
      { 
       using (StreamReader sr = new StreamReader(fs)) 
       { 
        while (!sr.EndOfStream) 
         items.Add(sr.ReadLine()); 

        foreach (string item in items) 
        { 
         var row = dt.NewRow(); 
         string[] columnValues = item.Split(','); 
         int column = 3; 

         for (int a = 0; a < columnValues.Length; a++) 
         { 
          string date = columnValues[29] + " " + columnValues[28]; 
          row[col[1].ColumnName] = DateTime.Parse(date); 
          string test = file.Split(new[] { splitVal }, StringSplitOptions.None)[1]; 
          row[col[2].ColumnName] = test.Split('.')[0]; 

          if (a >= 54) 
          { } 
          else 
          { 
           if (string.IsNullOrEmpty(columnValues[a])) 
           { 
            row[col[column].ColumnName] = DBNull.Value; 
           } 
           else 
           { 
            try 
            { 
             try 
             { 
              row[col[column].ColumnName] = columnValues[a].Trim(); 
             } 
             catch 
             { 
              try 
              { 
               row[col[column].ColumnName] = Convert.ToDouble(columnValues[a].Trim()); 
              } 
              catch 
              { 
               row[col[column].ColumnName] = int.Parse(columnValues[a].Trim()); 
              } 
             } 
            } 
            catch (Exception ex) 
            { 
             Console.WriteLine("Error [" + col[column].ColumnName + "]:" + ex.ToString()); 
             //row[col[column].ColumnName] = DBNull.Value; 
            } 
           } 
          } 

          column++; 
         } 
         dt.Rows.Add(row); 
        } 

        using (SqlConnection con = new SqlConnection(consstring)) 
        { 
         using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con)) 
         { 
          //Set the database table name 
          sqlBulkCopy.DestinationTableName = dbTable; 
          con.Open(); 
          try 
          { 
           sqlBulkCopy.WriteToServer(dt); 
           Console.WriteLine(file.Substring(file.LastIndexOf('\\') + 1) + " uploaded in the database\n"); 
          } 
          catch (Exception ex) 
          { 
           Console.WriteLine(file.Substring(file.LastIndexOf('\\') + 1) + " cannot be uploaded to database. " + ex.ToString()); 
          } 
          con.Close(); 
         } 
        } 
        sr.Dispose(); 
        sr.Close(); 
       } 

       fs.Dispose(); 
       fs.Close(); 
      } 
     } 
    } 
} 

上記のコードは、WinSCPのSession.SynchronizeDirectories Methodに基づいています。

答えて

0

*.csvすべてのファイルを列挙しないでください。ダウンロード/同期されたものだけを列挙:

foreach (TransferEventArgs transfer in synchronizationResult.Downloads) 
{ 
    string file = transfer.Destination; 
    ... 
} 

SynchronizationResult classを参照してください。

+0

ありがとうございます。コードをRunメソッドから 'FileStream fs = new FileStream(file、FileMode.Open、FileAccess.Read、FileShare.ReadWrite)){...}'を使ってコードに移動しました。したがって、ダウンロードされるすべての新しいファイルに対して、TransferEventArgsは自動的に正しく実行されますか? –

+0

* "TransferEventArgs **は自動的に実行されます" *とはどういう意味ですか? –

+0

私は同期がランタイムであり、リモートサーバーから新しいファイルがダウンロードされるたびに、そのforeachループが新しいファイルを自動的に検出することを意味していますか? –

関連する問題