본문 바로가기
C#(CSharp)/Etc

IOException: The process cannot access the file 'file path' because it is being used by another process

by swconsulting swconsulting 2018. 6. 23.
private const int NumberOfRetries = 3;
private const int DelayOnRetry = 1000;

for (int i=1; i <= NumberOfRetries; ++i) {
    try {
        // Do stuff with file
        break; // When done we can break loop
    }
    catch (IOException e) when (i <= NumberOfRetries) {
        // You may check error code to filter some exceptions, not every error
        // can be recovered.
        Thread.Sleep(DelayOnRetry);
    }
}

SOURCE : https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being





        private bool imageFileMove(string src, string dest)

        {

            int NumberOfRetries = 15;

            int DelayOnRetry = 500;

            bool isMoveOk = true;


            showTryCount( 0 );


            for (int i=1; i <= NumberOfRetries; ++i)

            {

                try {

                    // Do stuff with file

                    File.Move( src, dest );


                    break; // When done we can break loop

                }

                catch (IOException e) when( i <= NumberOfRetries)

                {

                    showTryCount( i );


                    Thread.Sleep( DelayOnRetry );

                    Debug.WriteLine( e.Message );


                    if ( i == NumberOfRetries )

                        isMoveOk = false;

                }

            }


            return isMoveOk;

            

        }


        private void showTryCount( int i )

        {

            App.Current.Dispatcher.Invoke( (Action)delegate // <--- HERE

            {

                FileMoveCount.Content = "File Move Try Count : " + i + ", ImageList [Index:Count] => " + imgIndex + ":" + nullSchoolImageList.Count;

            } );


            Thread.Sleep( 300 );

        }



댓글0