Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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#_Directory_File Exists_Visual C# Express 2010 - Fatal编程技术网

C# 正在等待目录C中存在文件

C# 正在等待目录C中存在文件,c#,directory,file-exists,visual-c#-express-2010,C#,Directory,File Exists,Visual C# Express 2010,我正在创建一个Visual C应用程序,它的部分功能是在.gz文件出现在目录中时提取它。执行命令行参数后,.gz文件将显示在指定的目录中 不幸的是,我收到一个错误,说找不到与此文件相关的内容,这是因为它读取该行以提取.gz文件太快 换句话说,它试图在命令行参数执行之前执行一个.gz文件,并实际将该文件放入目录中 我想找到一种方法,让我的程序在继续读取下一行之前,等待文件出现在目录中 下面是我的代码,任何帮助都将不胜感激!谢谢 else if (ddlDateType.Text == "Month

我正在创建一个Visual C应用程序,它的部分功能是在.gz文件出现在目录中时提取它。执行命令行参数后,.gz文件将显示在指定的目录中

不幸的是,我收到一个错误,说找不到与此文件相关的内容,这是因为它读取该行以提取.gz文件太快

换句话说,它试图在命令行参数执行之前执行一个.gz文件,并实际将该文件放入目录中

我想找到一种方法,让我的程序在继续读取下一行之前,等待文件出现在目录中

下面是我的代码,任何帮助都将不胜感激!谢谢

else if (ddlDateType.Text == "Monthly" || ddlDateType.Text == "")
{
    //Check if Monthly date entered is valid
    if (DateTime.TryParseExact(txtDate.Text, MonthlyFormat, null,
        System.Globalization.DateTimeStyles.None, out Test) != true)
    {
        MessageBox.Show("Enter a valid date.\nFormat: yyyyMM");
    }
    else
    {
        //Method that executes an arugment into the command prompt
        ExecuteCommand();

        //Method that extracts the file after it has already appeared in the directory
        ExtractFile();

        /*
        Goal is to wait for the file to appear in the directory before it executes
        the ExtractFile() method.
         */

    }
}

您可以使用FileSystemWatcher

在本例中,每次将文件添加到受监视的文件夹时,都会调用回调OnChanged

   [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    public static void RunWathcer()
    {

        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();

        watcher.Path = "PATH TO WATCH GOES HERE!!";

        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        watcher.Filter = "*.*";

        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
    }

    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
       Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
    }
}

您可以使用FileSystemWatcher

在本例中,每次将文件添加到受监视的文件夹时,都会调用回调OnChanged

   [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    public static void RunWathcer()
    {

        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();

        watcher.Path = "PATH TO WATCH GOES HERE!!";

        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        watcher.Filter = "*.*";

        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
    }

    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
       Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
    }
}

我的第一个想法就是做一会儿!File.exists文件{System.Threading.Thread.Sleep1000;}我的第一个想法是暂时停止!File.exists文件{System.Threading.Thread.Sleep1000;}扮演魔鬼代言人:如果此目录路径中更改了所需文件以外的内容,会发生什么情况?那么它不会失败吗?显然,您必须筛选预期的文件名等等。。。我提出了一个可能的解决方案,我没有看到所有的程序,我无法预测:-我不知道这个FileSystemWatcher类存在。我认为这很有用@Gary The watcher.Filter看起来可以设置为在名称为动态的情况下查找特定的文件或文件扩展名。是的,对,关于如何在此处查找的详细信息:@DaniloCalzetta Twas一点也不挑剔,我只是问:。扮演恶魔的倡导者:如果此目录路径中更改了所需文件以外的内容,会发生什么?那么它不会失败吗?显然,您必须筛选预期的文件名等等。。。我提出了一个可能的解决方案,我没有看到所有的程序,我无法预测:-我不知道这个FileSystemWatcher类存在。我认为这很有用@Gary The watcher.Filter看起来可以设置为在名称为动态的情况下查找特定的文件或文件扩展名。是的,对,关于如何在此处查找的详细信息:@DaniloCalzetta TWA一点也不挑剔,我只是问:。