Night Walker
Night Walker

Reputation: 21260

copy a file to shared folder on another computer

I am trying copy file to mapped network location. If I try to do it manually everything is working OK.

By running following code I don't get any exceptions but I not get the code at the needed location.

     string _sharedLocation = @"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";

     if (Directory.Exists(_sharedLocation) && File.Exists(@"c:\\Automation\\Tests\\Test1\\events.json"))
     {
         File.Copy(@"c:\\Automation\\Tests\\Test1\\events.json", Path.Combine(_sharedLocation, "events11.json"), true);
     }

Any suggestions with that issue.

Upvotes: 1

Views: 10303

Answers (4)

Flater
Flater

Reputation: 13773

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");

Upvotes: 1

Hassan
Hassan

Reputation: 5430

Problem: Your shared Path refers to C: drive of same machine. Possibly you are referring to the shortcut of mapped network location.

 string _sharedLocation = @"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";

It should be:

 string _sharedLocation = @"\\ComputerNetworkIdentity\SharedFolder\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";

Shared computer can be located using \\ComputerName. You must have Write permission on shared folder.

Simple Way Locate A Shared Folder:

  1. Open Run dialog.
  2. Type computer name with preceding backslash e.g. \\ComputerNetworIdentity
  3. Locate the folders shared by the network computer.

Upvotes: 0

Harikant
Harikant

Reputation: 277

I guess you sharedLocation path is not valid.

If you write @"c:\" it will refer your local drive on which the code is running so Please correct it

Upvotes: 0

Codor
Codor

Reputation: 17605

It seems that the target path _sharedLocation also refers to a local path, not a remote path.

Upvotes: 0

Related Questions