Solved

try file move max 5 times

Posted on 2011-09-12
2
299 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video teaches viewers about errors in exception handling.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

756 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