Solved

try file move max 5 times

Posted on 2011-09-12
2
296 Views
Last Modified: 2012-05-12
I want my program to move files from source folder to destination folder, but I get IO exception sometimes when the file is being accessed by another program. So, I want my program to make 5 tries if it encounter IO Exception with a gap of 1 min. how would I do that? It sound simple, but I am confused on how I would loop the try catch block max 5 times and come out of it if it was successful in less then 5 attempts.
Move_The_Files(ActiveWorkFolder + SubDirName, DestinationPath + SubDirName);

Open in new window


Thanks for your help.
0
Comment
Question by:patd1
2 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36523054
Perhaps something like this:
for (int i = 0; i < 5; i++)
{
    try
    {
        Move_The_Files(ActiveWorkFolder + SubDirName, DestinationPath + SubDirName);
        break;
    }
    catch (IOException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Open in new window

0
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 36523116

public void MoveFile( string fromFileName, string toFileName, int numberOfRetries )
        {
            int numberOfSecondsBetweenRetries = 60;
            int retries = 0;

            while ( retries < numberOfRetries )
            {
                if ( moveFile( fromFileName, toFileName ) )
                {
                    retries = numberOfRetries;
                }
                else
                {
                    System.Threading.Thread.Sleep( numberOfSecondsBetweenRetries * 1000 );
                    retries++;
                }
            }
        }

        private bool moveFile( string fromFileName, string toFileName )
        {
            bool didSucceed = false;

            try
            {
                System.IO.File.Move( fromFileName, toFileName );
                didSucceed = true;
            }
            catch ( System.IO.IOException ioEx )
            {
                // do something with ioEx perhaps
                didSucceed = false;
            }
            catch ( Exception ex )
            {
                // Do somthing with ex perhaps
            }

            return didSucceed;
        }

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now