1

When I try to copy an existing file from a remote computer to local with

File.Copy(
    string.Format(@"\\{0}\e$\{1}", computerName, fileName), 
    string.Format(@"{0}\{1}\{2}", localPath, computerName, fileName), 
    true);

I get the exception

Could not find part of the path "\computername\e$\filename".

I checked the path and it's correct.

I don't think that is a permission problem because I can reach the file with Directory.GetFiles and I can obtain info like file size or last writing date with FileInfo, moreover when I execute xcopy command from cmd with the same paths in the code he copies the file successfully.

Can anyone help me to understand what I do wrong or other ways to copy file?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Are you running that file copy as you as a user, or from a web page, a service? – BugFinder Apr 07 '16 at 08:21
  • @BugFinder I'm running file copy in a c# console project, so as me as user. – Gianluca Baroetto Apr 07 '16 at 08:23
  • you can try to use "net use". After connect with the drive, you can copy the file. – naro Apr 07 '16 at 08:26
  • ok, but, you have \\computername +e$ +filename, and localpath +computername +file .. are you sure the file exists on the remote server? can you brows to \computername\e$ - show an example filename and file list on the e$ of the remote server.. – BugFinder Apr 07 '16 at 08:26
  • Build your paths with [`Path.Combine`](https://msdn.microsoft.com/de-de/library/dd991142(v=vs.110).aspx) to make sure your paths are created correctly. – diiN__________ Apr 07 '16 at 08:33
  • @BugFinder Yes, I'm sure the file exists beacuse I can open the file system on the remote computer and check it. – Gianluca Baroetto Apr 07 '16 at 08:39
  • @diiN_ I've already tried to use `Path.Combine`. I don't think path is wrong because the Exception message print the correct path that I checked on cmd and works, this is the thing that I dont understand. – Gianluca Baroetto Apr 07 '16 at 08:48
  • So, show the value of computername, localpath and filename when it fails - Im guessing part of it is wrong such as filename has more info in it than you think – BugFinder Apr 07 '16 at 08:55
  • So having run a test, I can only conclude your path/filename combo is incorrect somehow, as my own copy from unc to local and local to unc works just fine. – BugFinder Apr 07 '16 at 09:04

2 Answers2

0

Copying from or to a remote location is not done by using the normal File.Copy since there's a difference if the files are saved on the local HDD or if they are accesible by your network. Other community members already worked out a few solutions. The easiest one might be this:

How to provide user name and password when connecting to a network share

another solution is provied in this thread:

Copying a file to a network share i dont have access to

You need to find out which suits you best. It's important to mention that your program might not be runnable from everywhere and by everyone if you skip the Impersonation. Which could prevent users from a failure free working.

Community
  • 1
  • 1
prizm1
  • 363
  • 1
  • 11
  • Actually normal file.copy does work to unc - I just did it. – BugFinder Apr 07 '16 at 09:01
  • I take it all back then :) I remembered I used to have to do the same and didn't get it to work without those specialized ways. But if it's possible to achieve easier, why not stick to that then. – prizm1 Apr 07 '16 at 09:05
  • I think there are times it may not work - such as if you havent been to that UNC then you may not have verified your connection and it may fail the copy, however, as long as your PC and the remote server have previously talked about this, it would work.. However, I think the OP's issues are more simple. – BugFinder Apr 07 '16 at 09:09
0

I discovered that File.Copy doesn't create the given destination folder if it doesn't exist, unlike xcopy command. This confused me because I supposed that if cmd command creates the dir also Copy method can manage this case. Moreover the Exception message specified that the source path was wrong instead of the destination path.

So to solve the problem:

localPath = Path.Combine(localPath, computerName);
if (!Directory.Exists(localPath))
{
    Directory.CreateDirectory(localPath);
}
File.Copy(
string.Format(@"\\{0}\e$\{1}", computerName, fileName), 
Path.Combine(localPath, fileName), 
true);

I apologize for my not so good english and for distraction.

Thanks for the help.