我是 C# 的新手,我确信我正在编写一个程序,如果由于现有 3rd 方程序的限制,它们满足特定的时间标准,则可以移动文件。该程序以其当前容量工作,但是我想让它检查目标中具有相同名称的文件并更改正在移动的文件的名称,因此如果已经存在具有一样的名字。在自定义类 GloDir 中还声明了一些 fname 和 target,并在其他函数中使用。
这有效,但不检查目标中的文件;
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
}
}
为了检查目标中的文件,我将其更改为添加一个 If 语句,该语句检查目录中的任何文件,然后是一个嵌套的 foreach 循环,该循环比较值并且也可以工作。唯一的问题是当尝试在循环中使用字符串 declard 或 if 语句时,我复制了代码并添加了以下内容。我还留下了一些控制台写入和睡眠,让我可以看到最终代码中不会出现的程序功能;
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# 的新手,任何帮助表示赞赏。