Solved

try file move max 5 times

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using sqldatareader to populate cells in Excel-syntax 6 36
VB.NET - Refactor Class per SOLID principles 2 43
aspx ascx, c# 7 45
junit initializtion error 2 22
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

752 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