Solved

FileInfo CopyTo Method Fails through mapped drive

Posted on 2006-06-16
6
1,284 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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