[Solved] Copying local file to network shared drive issues


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 (i.e. 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 foundthat 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.

solved Copying local file to network shared drive issues