Reputation: 73
In c# 2008, I'm trying to copy a file to a destination path (for example \newserver\destinationFolder), that can be in another domain or using a different username/password than the current user. How can I specify these new network credentials before doing the File.Copy(...) ?
Thank you!
Upvotes: 7
Views: 6465
Reputation: 16508
MapDrive => Copy files as local user => UnmapDrive. Take a look here:
protected int MapDrive(){
NETRESOURCEA[] resources = new NETRESOURCEA[1];
resources[0] = new NETRESOURCEA();
resources[0].dwType = 1;
int dwFlags = 1;
resources[0].lpLocalName = _DriveLetter;
resources[0].lpRemoteName = _Path;
resources[0].lpProvider = null;
int ret = WNetAddConnection2A(resources, null, null, dwFlags);
return ret;
}
protected int UnmapDrive()
{
int ret = WNetCancelConnection2A(_DriveLetter, 0, true);
return ret;
}
Upvotes: 0
Reputation: 55009
Look at LogonUser. Here's the pinvoke page for it:
http://www.pinvoke.net/default.aspx/advapi32.logonuser
Upvotes: 1
Reputation: 86882
Good question but I dont think this is possible. I believe this type of file copy from one domain to another ( where there is not a trust set up) would be considered security hole.
Some options:
Upvotes: 2