Solved

try file move max 5 times

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

8 Experts available now in Live!

Get 1:1 Help Now