views:

1179

answers:

4

I'm attempting to use File.Move to move a file from one UNC location to another. This blows up if the UNC path for the destination happens to be the local machine (error: Access to the path is denied). Example File.Move(@"\someServer\path\file.txt", @"\blah2\somewhere\file.txt"). This assumes there's a network share out there somewhere named \someServer and my local machine name is blah2. Change \blah2 to C:\ and all is good.

A: 

Shouldn't the server name in your unc path start with two slashes?
eg @"\\someServer\path\file.txt"

hamishmcn
Given that I had to write three slashes for two to appear on my post, I now guess that you had the same problem, and that you do have the paths correct. Sorry I couldn't have been more helpful :-)
hamishmcn
+3  A: 

Maybe it's a typo, but shouldn't it be @"\\blah2\somewhere\file.txt", i.e. with two backslashes?

Juanma
A: 

I can't seem to reproduce the bug. Maybe it has something to do with permissions? Can you move to anywhere on the local machine using a UNC address or do they all throw the exception?

ICR
+1  A: 

For a Windows file share, write access requires permission to be specified in one additional place (other than the ACL) - in the share permissions. If you are using an administrative share like C$ then write access is already enabled, but if you are using a share that has been explicitly set up for this, ensure that the "Change" option is enabled under "Share Permissions".

Note that you can often troubleshoot permission errors by checking the Security event log under Computer Management -> Event Viewer, there may be additional details on how it is failing. Enabling the auditing of logon failures under Administrative Tools -> Local Security Policy -> Local Policies -> Audit Policy may also provide more detail in the event log.

J c