Shashank Kolpuru 5 days ago

Retry the File for three times If the File Exists through Recursive Method 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 :

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.

Answers


SirBirne 5 days ago

I think you want something like this:

public bool 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");

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

    if (!exists)
    {
        Console.WriteLine("File uninstalled");
        return true;
    }
    return false;

}

Post Status

Asked in 5 days ago
Viewed 1,881 times
Voted 10
Answered 1 times

Search




Leave an answer