FileInfo CopyTo Method Fails through mapped drive

Posted on 2006-06-16
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)
Question by:snogopher
  • 3
  • 3

Author Comment

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

Expert Comment

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

Expert Comment

ID: 16924844
Can you also try using,

  fi1.CopyTo(fi2.FullName, true);


  fi1.CopyTo(saveBackupDialog.FileName, true);
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.


Author Comment

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";
LVL 11

Accepted Solution

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

  while (!fi1.Exists)

  fi1.CopyTo( fi2.FullName, true );

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

11 Experts available now in Live!

Get 1:1 Help Now