2012-08-01 5 views
0

두 개의 코드를 통합하는 데 문제가 있습니다. 첫 번째 파일의 크기를 확인하고 다음 하나는 SQL 데이터베이스를 반복하고 파일에 대해 일치하는 이름을 찾습니다. 기본적으로 새 파일인지 또는 파일 중 일부가 마지막으로 기록 된 이후 파일이 변경된 경우 확인하고 싶습니다.파일의 이름과 크기 맞추기

이이 코드가 저점을 보았다 된 기계의 또는 전까지 모든 항목을 찾아 전까지 루프 디렉토리

// Make a reference to a directory. 
     DirectoryInfo di = new DirectoryInfo("C:\\Users"); 
     // Get a reference to each file in that directory. 
     FileInfo[] fiArr = di.GetFiles(); 
     // Display the names and sizes of the files. 
     MessageBox.Show("The directory {0} contains the following files:", di.Name); 
     foreach (FileInfo f in fiArr) 
      MessageBox.Show("The size of" + f.Name + " is " + f.Length + " bytes."); 

에 각 파일의 크기를 가져옵니다.

try 
      { 
       // LINQ query for all files containing the word '.txt'. 
       var files = from file in 
           Directory.EnumerateFiles("C:\\Users") 
          where file.ToLower().Contains(".txt") 
          select file; 

      foreach (var file in files) 
      { 
       //Get path to HH file 
       filename = System.IO.Path.GetFileName(file); 

       tempString = ""; 

       //Keep looking trough database utill database empty or HH found 
       while (inc != numberOfSessions && (filename != tempString)) 
       { 

        sessionRow = sessions.Tables["Sessions"].Rows[inc]; 
        tempString = sessionRow.ItemArray.GetValue(1).ToString(); 

        inc++; 
       } 

ItemAttay.GetValue (2)는 저장된 파일 크기를 반환합니다. 어떻게 가장 효율적으로

경우 INC가는 while 루프를 유지할 수 있습니다! = numberOfSessions & & (파일 이름을! = tempString) & & (sessionRow.ItemArray.GetValue (2) == f.length)

감사에 대한 봐라!

+0

파일 내용이 변경되었지만 크기가 동일해도 상관 없습니까? 그렇다면 "파일 크기가 변경되었는지 확인"기준이 작동하지 않습니다. 대신 해시를 계산할 수 있습니까? –

+0

새 파일인지 또는 파일에 추가 된 파일인지 알기가 어렵습니다. 절대 (결코) 나는 더 작아 질 것입니다. 해시 계산 방법에 대해 어떻게 생각하십니까? –

+0

기본 .NET 해시 함수의 기본 클래스는 [HashAlgorithm]입니다 (http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx). http://support.microsoft.com/kb/307020에서 컴퓨팅 파일 해시를 설명하는 몇 가지 정말 오래된 설명서가 있습니다. –

답변

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Data; 



    class Program 
    { 
     static void Main(string[] args) 
     { 
      var files1 = new List<string>(Directory.GetFiles(args[0], 
       "*.txt", 
       SearchOption.AllDirectories)); 


      List<FileData> ListFiles = new List<FileData>(); 

      for (int i = 0; i < files1.Count; i++) 
      { 


      FileInfo file = new FileInfo(files1[i]); 
      FileData _tmpfile = new FileData(file.Name.ToString(), file.Length, 
       File.GetLastWriteTime(files1[1]).ToString("yyyy-MM-dd H:mm:ss"), 
       File.GetLastAccessTime(files1[1]).ToString("yyyy-MM-dd H:mm:ss")); 
      ListFiles.Add(_tmpfile); 
      } 

      DataSet sessions = new DataSet(); 
      DataTable dt = sessions.Tables["Sessions"]; 
      for (int i = 0; i < ListFiles.Count; i++) 
      { 
       //compares every file in folder to database 
       FileData _tmp = ListFiles[i]; 
       for (int j = 0; j < dt.Rows.Count; j++) 
       { 
        if (_tmp.GSFileName == dt.Rows[i][0].ToString()) 
        { 
         //put some code here 
         break; 
        } 

        if (_tmp.GSSize == long.Parse(dt.Rows[i][1].ToString())) 
        { 
         //put some code here 
         break; 
        } 

       } 


      } 



     } 
    } 
    public class FileData 
    { 
     string FileName = ""; 

     public string GSFileName 
     { 
      get { return FileName; } 
      set { FileName = value; } 
     } 
     long Size = 0; 

     public long GSSize 
     { 
      get { return Size; } 
      set { Size = value; } 
     } 
     string DateOfModification = ""; 

     public string GSDateOfModification 
     { 
      get { return DateOfModification; } 
      set { DateOfModification = value; } 
     } 
     string DateOfLastAccess = ""; 

     public string GSDateOfLastAccess 
     { 
      get { return DateOfLastAccess; } 
      set { DateOfLastAccess = value; } 
     } 

     public FileData(string fn, long si, string dateofmod, string dateofacc) 
     { 
      FileName = fn; 
      Size = si; 
      DateOfModification = dateofmod; 
      DateOfLastAccess = dateofacc; 

     } 

    } 
+0

Brilliant! 고마워요. 그게 내가 찾고 있던 바로 그거야! –