Solved

FileInfo CopyTo Method Fails through mapped drive

Posted on 2006-06-16
6
1,294 Views
Last Modified: 2007-12-19
I am trying to use the fileinfo copyto method to copy a file from a mapped drive to the local machine, when i run it, it breaks with a file not found exception, if i just hit continue after it breaks it then works fine. It seems that it is not waiting long enough to find the mapped drive?

code snippet:

FileInfo fi1 = new FileInfo( "J:\\BackUp-" + time.ToLongDateString().Remove( 0, time.ToLongDateString().IndexOf( " ", 0, 20 ) ).Trim().Replace( ",", "" ) + ".BAK" );

FileInfo fi2 = new FileInfo( saveBackupDialog.FileName );
                 
fi1.CopyTo( fi2.Name, true );

I have tried inserting a pause like: System.Threading.Thread.Sleep( 5000 ); between the creation of the file and copying the file but that does not work. (The file is created just before it is copied)
0
Comment
Question by:snogopher
[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
  • 3
  • 3
6 Comments
 

Author Comment

by:snogopher
ID: 16923298
I have increased the point value as i have tried everything i can think of to no avail.
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 16924835
Can you show the code where the file is created?
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 16924844
Can you also try using,

  fi1.CopyTo(fi2.FullName, true);

or,

  fi1.CopyTo(saveBackupDialog.FileName, true);
0
Independent Software Vendors: 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!

 

Author Comment

by:snogopher
ID: 16934939
i tried those and it did not work, here is more of the code

what this code does is have the sql server perform a backup to its local drive which i have mapped as the j drive from my dev machine and i hant to copy that file to my dev machine after the sql server performs the backup.

when i run the code it breaks on the copyto method with a filenotfoundexception and when i hit continue after it breaks it works fine

// Creates backup device
BackupDeviceItem bkpDevice = new BackupDeviceItem( "C:\\BackUps\\BackUp-" + time.ToLongDateString().Remove( 0, time.ToLongDateString().IndexOf( " ", 0, 20 ) ).Trim().Replace( ",", "" ) + ".BAK", DeviceType.File );

                    // Add the backup device to the backup
                    bkpDatabase.Devices.Add( bkpDevice );

                    // Perform the backup
                    bkpDatabase.PercentComplete += new PercentCompleteEventHandler( rstDatabase_PercentComplete );
                    bkpDatabase.SqlBackup( srvSql );
                    bkpDatabase.PercentComplete -= rstDatabase_PercentComplete;

                   
                    FileInfo fi1 = new FileInfo( "J:\\BackUp-" + time.ToLongDateString().Remove( 0, time.ToLongDateString().IndexOf( " ", 0, 20 ) ).Trim().Replace( ",", "" ) + ".BAK" );

                    FileInfo fi2 = new FileInfo( saveBackupDialog.FileName );
                    fi1.CopyTo( fi2.FullName, true );
                    toolStripStatusLabel1.Text = "Back-Up Successful";
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 16935726
You could try this:

  while (!fi1.Exists)
  {
    System.Threading.Thread.Sleep(250);
    fi1.Refresh();
  }

  fi1.CopyTo( fi2.FullName, true );
0
 

Author Comment

by:snogopher
ID: 16935774
That works perfectly, also i did a check with an int and it runs through that loop 18 times before it finds the file
0

Featured Post

Independent Software Vendors: 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 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