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

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

by 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 );

        }



'C#(CSharp) > Etc' 카테고리의 다른 글

Single Global Mutex  (0) 2019.06.25
Easily write a whole class instance to XML File and read back in  (0) 2018.06.25
C# memory cache logic  (0) 2018.05.31
Write exception log in File.  (0) 2018.05.29
c# memory cache  (0) 2018.05.26