Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FileInfo CopyTo Method Fails through mapped drive

Posted on 2006-06-16
6
Medium Priority
?
1,328 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
Industry Leaders: 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

783 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