Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据函数C中的数组检查数组的值#_C#_Arrays_File Io_Console Application_Scope - Fatal编程技术网

C# 根据函数C中的数组检查数组的值#

C# 根据函数C中的数组检查数组的值#,c#,arrays,file-io,console-application,scope,C#,Arrays,File Io,Console Application,Scope,我是C#新手,我确信我正在编写一个程序,如果由于现有第三方程序的限制,文件满足特定的时间标准,则会移动这些文件。该程序在其当前容量下工作,但是我想让它检查目标中具有相同名称的文件,并更改正在移动的文件的名称,这样,如果已经存在具有相同名称的文件,则该程序不会出错。另外,一些fname和target在自定义类GloDir中声明,并在其他函数中使用 这可以工作,但不检查目标中的文件 DirectoryInfo sourceInfo = new DirectoryInfo(GloDir.fname);

我是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,以便可以在if语句和循环之外看到它?我确信这是一个简单的错误,或者是我还没有学会的错误。这也可能是变量范围的问题,但考虑到另一个变量也是字符串,我不这么认为。我再次对C#不熟,非常感谢您的帮助。

这是错误的:

        if (destFiles.Length == 0)
        {
                string newFilename = fileName;
        }
newFilename
仅存在于if测试中

替换为:

string newFilename = string.Empty;
if (destFiles.Length == 0)
{
    newFilename = fileName;
}
之后你就可以使用它了

您已经在代码中多次这样做了


这篇关于MSDN的文章包含了您可能需要的关于此主题的所有信息。

回答得好,但也值得指向上的MSDN文章。这解决了问题。非常感谢你!现在我有了另一个,它不断地将newFilename命名为filename,即使目标中有重复的文件。我认为这现在转移到程序流,但我不明白为什么它不会改变。你有什么建议吗?我意识到产生的错误是一个简单的流程。我给了sting一个值,只有在满足该条件时才更改它,而不是尝试在所有条件下更改它。谢谢大家。@Default抱歉,删除了注释…h8a您能在发布的第二个示例的完整方法中显示代码吗。如果您希望使用相同的destInfo、destFiles和sourceInfo,然后,您需要声明在方法范围之外创建一次新的外部,如果它将在两种方法中被引用,我希望我正确理解您的问题
您可能希望使用Ref
键查看word@DJKRAZE最后,这将只是第二种方法。一旦它起作用,就应该变成第二个。这回答了你的问题吗?