我是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循环,用于比较值并且也可以。唯一的问题是当尝试在循环中使用字符串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进行delcare,以便可以在if语句和循环之外看到它?我确信这是一个简单的错误,或者我尚未学到的错误。这也可能是变量范围的问题,但我不这么认为,考虑到另一个变量也是一个字符串。我再次成为C#的新手,感谢任何帮助。
答案 0 :(得分:1)
这是错误的:
if (destFiles.Length == 0)
{
string newFilename = fileName;
}
newFilename
仅在内 if test。
将其替换为:
string newFilename = string.Empty;
if (destFiles.Length == 0)
{
newFilename = fileName;
}
然后你就可以使用它了。
您已在代码中多次执行此操作。
关于Variable Scope的这篇MSDN文章包含了您可能需要的有关此主题的所有信息。