Solved

try file move max 5 times

Posted on 2011-09-12
2
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

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 20

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Part One of the two-part Q&A series with MalwareTech.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

623 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