简体   繁体   中英

Copy a file from local drive to shared drive using C#

I want to copy a file from my local drive to a shared network path.

I have tried the following way:

string remoteUserName = 
          WebConfigurationManager.AppSettings["remoteUsername"].ToString();
string remotePassword =
          WebConfigurationManager.AppSettings["remotePassword"].ToString();
string remoteDomain = 
          WebConfigurationManager.AppSettings["remoteDomain"].ToString();
string remoteFilePath = 
          WebConfigurationManager.AppSettings["remoteFilePath"].ToString();

using (var impersonation = new 
          ImpersonatedUser(remoteUserName, remoteDomain, remotePassword))
{
    CreateErrorLog("Logged in successfully - User and password are correct.", 
                   "Action" + " - " + "controllerName");
    string filePath = remoteFilePath;
    string fileName = "txt.txt";
    StreamWriter SW1;
    FileIOPermission myPerm = new 
               FileIOPermission(FileIOPermissionAccess.AllAccess, filePath + fileName);
    myPerm.Assert();
    SW1 = System.IO.File.CreateText(filePath + fileName);
}

Ok, let's work on this code a little. First let's simplify building the paths. We have a network path and a local path. According to your current code the network path is built with a few variables comboBox1, comboBox2, and Environment.UserName, so let's do it a little different:

var networkPath = Path.Combine(@"\\network",
    comboBox1.SelectedItem as string,
    comboBox2.SelectedItem as string,
    Environment.UserName);

that's going to place the \\ in between each of those strings properly (ie if there were already a back slash it wouldn't add one, but would if necessary).

Now let's do the same for the local path:

var localPath = Path.Combine(@"C:\Users",
    Environment.UserName,
    "test",
    label5.Text);

ok, we're almost there, but we also have an alternative network path:

var alternativeNetworkPath = Path.Combine(@"\\atlanta2-0\it-documents\filestroage",
    comboBox1.SelectedItem as string,
    comboBox2.SelectedItem as string,
    Environment.UserName,
    label5.Text);

now, one thing about this path that's already suspect to me is this, \\filestroage, that's actually spelled wrong. Now, if the folder is spelled that way fine, but I'm wondering if it's spelled wrong. So just take a look. Alright, let's continue on, now we have all three paths built, it's a little easier to read, and we can easily output those strings to ensure they are right. Let's take a look at the logic. It says this, if the networkPath exists then save it there, however, if it does not exist then create it and save it to the alternativeNetworkPath. So let's do that:

if (Directory.Exists(networkPath))
{
    File.Copy(localPath, networkPath);
}
else
{
    Directory.CreateDirectory(networkPath);
    File.Copy(localPath, alternativeNetworkPath);
}

alright, simple enough yes? But you stated that the Directory.Exists is returning true even if it exists. That's pretty much expected isn't it? If the directory exists then this method would certainly return true, if not then it would return false. You then stated with the Directory.CreateDirectory that The line above says the network name cannot be found - that can only mean that the path was constructed wrong.

So after breaking it down, the bottom line is this, the paths being constructed have to be off a tidge. However, with this new model you should be able to pull those paths out a lot easier. So the entire method, in my mind, would look something like this:

var networkPath = Path.Combine(@"\\network",
    comboBox1.SelectedItem as string,
    comboBox2.SelectedItem as string,
    Environment.UserName);

var localPath = Path.Combine(@"C:\Users",
    Environment.UserName,
    "test",
    label5.Text);

var alternativeNetworkPath = Path.Combine(@"\\atlanta2-0\it-documents\filestroage",
    comboBox1.SelectedItem as string,
    comboBox2.SelectedItem as string,
    Environment.UserName,
    label5.Text);

if (Directory.Exists(networkPath))
{
    File.Copy(localPath, networkPath);
}
else
{
    Directory.CreateDirectory(networkPath);

File.Copy(localPath, alternativeNetworkPath);
}

and so now let's have a look at those paths in those variables and your problem should come right out.

Network paths are accessed by full Universal Naming Convention-UNC \\\\Server\\Share\\drive\\file paths. If you have these type credentials or rights to access network, You could use File.Copy method to move your files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM