Solved

FileInfo CopyTo Method Fails through mapped drive

Posted on 2006-06-16
6
1,275 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 162
I need help displaying 2 models in one view in my MVC.Net application with C# code behind 5 36
Runtime Error 2 29
Syntax Error 2 48
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

19 Experts available now in Live!

Get 1:1 Help Now