我是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#有所瞭解,並感謝任何幫助。
@Default對不起,刪除了評論... – 2013-03-12 14:49:11
h8a你可以在第二個例子中顯示代碼,你發佈它的完整方法。看起來如果你想針對相同的destInfo,destFiles和sourceInfo工作,那麼你需要聲明在方法範圍外創建新的外部一次,如果它將在兩個方法中被引用,我希望我是理解的你的問題正確'你可能想看看使用Ref'關鍵詞 – MethodMan 2013-03-12 14:49:39
@DJKRAZE最後它只是第二種方法。一旦它工作,它應該變成第二個。這是否回答你的問題? – h8a 2013-03-12 15:40:08