简体   繁体   English

重试文件3次如果文件通过递归方法存在并且在C#中返回布尔值

[英]Retry the File for three times If the File Exists through Recursive Method and which returns Boolean in C#

My Requirement is I Want My File to be Retried for 3 times If the Particular File Exists , I Have Achieved But the thing is I Want the Working Copy through Recursive Method which returns Boolean , Below Is my Working Code Which is not returned Boolean :我的要求是我希望我的文件重试 3 次如果特定文件存在,我已经实现但问题是我希望通过递归方法返回 Boolean 的工作副本,下面是我没有返回 Boolean 的工作代码:

public void Process(int count = 0)
    {
        bool exists = File.Exists("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\RevisionNumber.txt");

        if (exists && count < 3)
        {
            System.Diagnostics.Process.Start("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\SvnUninstallation.exe");
            Thread.Sleep(2000); // or long enough to ensure the uninstall process finishes executing

            //File exists
            Console.WriteLine("File exists");

            Process(++count);
        }
        else
        {
            Console.WriteLine("Exceeded retry of 3 times. File did not uninstall.");
        }

        if (!exists)
            Console.WriteLine("File uninstalled");

    }

Could Any One Help Me Out by Providing the solution through recursive method which returns boolean , and retry the file for three times if the file exists if not exists then execute only once.任何人都可以通过通过返回 boolean 的递归方法提供解决方案来帮助我,如果文件存在,则重试该文件 3 次,如果不存在,则只执行一次。

I think you want something like this:我想你想要这样的东西:

public bool Process(int count = 0)
{
    if (File.Exists("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\RevisionNumber.txt") && count < 3)
    {
        System.Diagnostics.Process.Start("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\SvnUninstallation.exe");
        Thread.Sleep(2000); // or long enough to ensure the uninstall process finishes executing

        //File exists
        Console.WriteLine("File exists");

        if(Process(++count))
           return true;
    }
    if (!File.Exists("C:\\Users\\sk185462\\Desktop\\SVNUPDATED\\RevisionNumber.txt"))
    {
        Console.WriteLine("File uninstalled");
        return true;
    }
    else
    {
        Console.WriteLine("Exceeded retry of 3 times. File did not uninstall.");
        return false;
    }
}

But you should definitly await the Process you started before continuing (not only by gussing time but by Process.WaitForExit()).但是你应该在继续之前明确等待你开始的进程(不仅是通过时间,而且是通过 Process.WaitForExit())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM