Copy File on a Network Shared Drive

Copy file on a network shared drive

Untested code, but it will be similiar to:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

// http://pinvoke.net/default.aspx/advapi32/LogonUser.html
IntPtr token;
LogonUser("username", "domain", "password", LogonType.LOGON32_LOGON_BATCH, LogonProvider.LOGON32_PROVIDER_DEFAULT);

WindowsIdentity identity = new WindowsIdentity(token);

WindowsImpersonationContext context = identity.Impersonate();

try
{
File.Copy(@"c:\temp\MyFile.txt", @"\\server\folder\Myfile.txt", true);
}
finally
{
context.Undo();
}

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

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 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.

copy a file to shared folder on another computer

looking at the _SharedLocation variable, it's on location: "...\Windows\Network Shortcuts\..."

I'm just guessing here, but are you tring to refer to a shortcut to a network folder, rather than a network folder?

This will never work:

File.Copy(myOriginalFile, "C:\...\MyShortcutToANetworkFolder\myFile.txt");

Why not? Because a shortcut is basically a file, not a folder (it's more complicated than that, but I'm keeping it simple for argument's sake). You cannot put a file (or anything else) into a shortcut. The only thing you can do with a shortcut is open it.

You need the actual network folder path.

This will work:

File.Copy(myOriginalFile, "\\myServer\myFolder1\myFolder2\myFile.txt");


Related Topics



Leave a reply



Submit