2013-03-12 53 views
0

我是C#的新手,我確定我正在編寫一個程序,在文件滿足一定的時間條件時移動文件,這是由於現有的第三方程序的限制。該程序以當前的能力工作,但是我想讓它檢查目標中具有相同名稱的文件,並更改正在移動的文件的名稱,以便在程序不存在錯誤的情況下一樣的名字。還有一些fname和target在自定義類GloDir中聲明,並在其他函數中使用。檢查函數C中數組對數組的值#

這可以工作,但不檢查目標文件;

DirectoryInfo sourceInfo = new DirectoryInfo(GloDir.fname); 
FileInfo[] sourceFiles = sourceInfo.GetFiles("*.zip");     
//creates array of all files ending in .zip 
DirectoryInfo destInfo = new DirectoryInfo(GloDir.target); 
FileInfo[] destFiles = destInfo.GetFiles("*.zip"); 

if (sourceFiles.Length == 0) // check to see if files are present. if not die. 
{ 
    return; 
} 

foreach (var sFileInfo in sourceFiles) 
{ 
    string sFip = sFileInfo.ToString();         
    //file info to string 
    string fileName = System.IO.Path.GetFileName(sFip);      
    //get file name 
    string sourceFile = System.IO.Path.Combine(GloDir.fname, fileName); 
    //Full filename and path 
    string targetFile = System.IO.Path.Combine(GloDir.target, fileName); 
    //New Target and Path 

    DateTime createdOn = File.GetCreationTime(sourceFile);    
    //get file creation time 
    DateTime rightNow = DateTime.Now;         
    //Variable for this moment 

    var difference = rightNow - createdOn; 
    //Different between now and creation 

    var minutos = difference.TotalMinutes; 
    //turn difference into minutes 

    //If time between creation and now is greater than 120 minutes move file to new directory 
    if (minutos >= 1)            
    { 
     //Console.Write(minutos + " Old - Moving! -");    
     //debug console msg 
     System.IO.File.Move(sourceFile, targetFile); 
     //move file to new directory 
     //System.Threading.Thread.Sleep(1555); 
     //debug sleep 
    } 
} 

要檢查目標我改變了它補充說,檢查目錄,然後任何文件嵌套foreach循環比較值並且還工作的if語句文件。唯一的問題是當試圖在循環中使用字符串declard時,或者如果我使用下面的添加語句複製了代碼。我還留下了一些控制檯寫入和睡眠,這讓我可以觀看那些不在最終代碼中的程序功能;

DirectoryInfo sourceInfo = new DirectoryInfo(GloDir.fname); 
FileInfo[] sourceFiles = sourceInfo.GetFiles("*.zip"); 
//creates array of all files ending in .zip 
DirectoryInfo destInfo = new DirectoryInfo(GloDir.target); 
FileInfo[] destFiles = destInfo.GetFiles("*.zip"); 

if (sourceFiles.Length == 0) 
// check to see if files are present. if not die. 
{ 
    //Console.Write("no files present"); 
    //debug console msg 
    return; 
} 

foreach (var sFileInfo in sourceFiles) 
{ 
    string sFip = sFileInfo.ToString();    
    //file info to string 
    string fileName = System.IO.Path.GetFileName(sFip);   
    //get file name 
    if (destFiles.Length != 0) 
    { 
     foreach (var dFileInfo in destFiles) 
     { 
      string dFip = dFileInfo.ToString(); 
      //file info to string 
      string dfileName = System.IO.Path.GetFileName(dFip);  
      //get file name 

      if (dfileName == fileName) 
      { 
       string newFilename = "Duplicate" + fileName; 
       Console.Write(newFilename + "dup change name"); 
       System.Threading.Thread.Sleep(1000); 
      } 
      else 
      { 
       string newFilename = fileName; 
       Console.Write(newFilename + "dest files no duplicate"); 
       System.Threading.Thread.Sleep(1000); 
      } 
     } 
    } 
    if (destFiles.Length == 0) 
    { 
     string newFilename = fileName; 
    } 
    string sourceFile = System.IO.Path.Combine(GloDir.fname, fileName); 
    //Full filename and path 
    string targetFile = System.IO.Path.Combine(GloDir.target, newFilename); 
    //New Target and Path 

    DateTime createdOn = File.GetCreationTime(sourceFile); 
    //get file creation time 
    DateTime rightNow = DateTime.Now;   
    //Variable for this moment 

    var difference = rightNow - createdOn; 
    //Different between now and creation 
    var minutos = difference.TotalMinutes; 
    //turn difference into minutes 

    //If time between creation and now is greater than 120 minutes move file to new directory 
    if (minutos >= 1)            
    { 
     System.IO.File.Move(sourceFile, targetFile); 
     //move file to new directory 
    } 
} 

每次它停止,並說,newFilename變量不會在目前的情況下存在,即使它應該根據目標目錄的條件下創建的時間。我是否需要對字符串newFilename進行不同的修飾,以便可以在if語句和循環之外看到它?我相信這是一個簡單的錯誤,或者我還沒有學到的東西。這也可能是一個變量範圍的問題,但我不會認爲考慮其他變量也是一個字符串。我再次對C#有所瞭解,並感謝任何幫助。

+0

@Default對不起,刪除了評論... – 2013-03-12 14:49:11

+0

h8a你可以在第二個例子中顯示代碼,你發佈它的完整方法。看起來如果你想針對相同的destInfo,destFiles和sourceInfo工作,那麼你需要聲明在方法範圍外創建新的外部一次,如果它將在兩個方法中被引用,我希望我是理解的你的問題正確'你可能想看看使用Ref'關鍵詞 – MethodMan 2013-03-12 14:49:39

+0

@DJKRAZE最後它只是第二種方法。一旦它工作,它應該變成第二個。這是否回答你的問題? – h8a 2013-03-12 15:40:08

回答

1

這是錯誤的:

 if (destFiles.Length == 0) 
     { 
       string newFilename = fileName; 
     } 

newFilename只存在 if測試。

將其替換爲:

string newFilename = string.Empty; 
if (destFiles.Length == 0) 
{ 
    newFilename = fileName; 
} 

然後你就可以以後使用它。

您已在代碼中多次完成此操作。

有關Variable Scope的此MSDN文章提供了有關此主題可能需要的所有信息。

+0

好的答案,但值得指向[變量範圍]上的MSDN文章(http://msdn.microsoft.com/zh-cn/library/ms973875.aspx)。 – pixelbadger 2013-03-12 14:48:17

+0

解決了這個問題。非常感謝你!現在我確實有了另外一個,即使在目標中存在重複,它也會將newFilename命名爲文件名。我認爲這現在轉向了程序流程,但我不明白爲什麼它不會改變。你有什麼建議嗎? – h8a 2013-03-12 17:34:01

+0

我意識到創建的錯誤是一個簡單的過程流程。我給了它一個價值,只有在滿足這個條件時才改變它,而不是試圖在所有條件下改變它。感謝大家。 – h8a 2013-03-13 19:59:45