[英]Checking Value of array against array inside function C#
我是C#的新手,并且我确定我正在编写一个程序,如果由于现有第三方程序的限制而满足一定的时间标准,它们将移动文件。 该程序以其当前容量工作,但是我想让它检查目标位置中具有相同名称的文件并更改要移动的文件的名称,因此如果程序中已经存在文件,则不会出错。一样的名字。 另外,一些fname和target在自定义类GloDir中声明,并在其他函数中也使用。
此方法有效,但不检查目标中的文件。
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循环比较这些值,并且该循环也有效。 唯一的问题是当尝试使用循环中声明的字符串时,或者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#的新手,感谢您的帮助。
这是错误的:
if (destFiles.Length == 0)
{
string newFilename = fileName;
}
newFilename
只存在了,如果测试里面 。
替换为:
string newFilename = string.Empty;
if (destFiles.Length == 0)
{
newFilename = fileName;
}
然后您就可以使用它了。
您已在代码中多次执行此操作。
这篇有关可变范围的 MSDN文章提供了您可能需要的有关此主题的所有信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.