The device is not ready when copying a file from one server to another

20,866

The problem is you're accessing un-mapped network location. When accessing network location, you need to map it (in some cases it's happend automatically, if you have the permissions. but if you don't have permissions you have to do it by yourself). You have few options:

  1. Work with mapped network drive and make sure it's always mapped (using group policy, or startup scripts, for example)
  2. Use Process.Start to run "net use" command (open cmd, type "net use" and see what you need to give). for example: Process.Start("c:\\windows\\system32\\net.exe", "use \\\\servername\\location /user:domain\\username password");
  3. Use impersonation in your code to impersonate to user with enough permissions before trying to access the network location.
Share:
20,866
JJ.
Author by

JJ.

Updated on October 05, 2020

Comments

  • JJ.
    JJ. over 3 years

    What am I doing wrong here?

    I have checked the variables and they are what they are supposed to be so no issues there.

    Am I missing something here? Why am I getting a "device is not ready" exception?

    Code -

    if (ddlPublisherServer.Text != ddlSubscriberServer.Text)
    {
       try
       {
           if (File.Exists("\\\\" + ddlSubscriberServer.Text + "\\SQLServerBackups\\" + txtSubscriberDatabaseName.Text + ".bak"))
           {
               File.Delete("\\\\" + ddlSubscriberServer.Text + "\\SQLServerBackups\\" + txtSubscriberDatabaseName.Text + ".bak");
           }
    
           File.Copy(@"D:\SQLServerBackups\" + txtSubscriberDatabaseName.Text, "\\\\" + ddlSubscriberServer.Text + "\\SQLServerBackups\\" + txtSubscriberDatabaseName.Text + ".bak");
           }
           catch (Exception ex)
           {
                ClientScript.RegisterStartupScript(GetType(), "Error!", "alert('" + ex.Message + "');", true);
                Logger.LogError(ex.Source, ex.TargetSite.ToString(), ex.Message);
                return;
           }
       }
    
  • JJ.
    JJ. over 11 years
    what is the best option and can you give me an example?
  • Shahar Gvirtz
    Shahar Gvirtz over 11 years
    are you on workgroup or domain? if you're in domain (active directory) environment and this application deployed only to users in the domain, simply make sure all the users has permissions to the network location. If it's workgroup, or you don't know for sure, then you'll have to get username and password as a parameter. In that case you can use this article: michiel.vanotegem.nl/2006/07/07/…
  • JJ.
    JJ. over 11 years
    Shahar. this did not work. this is what i used: Process.Start("c:\\windows\\system32\\net.exe", "use \\\\" + ddlSubscriberServer.Text + "\\SQLServerBackups\\ /user:mycompanydomain\\username password");
  • Shahar Gvirtz
    Shahar Gvirtz over 11 years
    you had any exception? did you try to copy what's actually running to cmd.exe, run it, and see if it works?