Coping a File

   I have a file that I copied to a local machine (under the ASP.NET process), but what I want to do now is make a copy of that file to some other location on the network, but because the current process is on the Web Server it is ASPNET local user, so they don't have access to other places on the network.
    So I need to impersonate another user and then make a copy of the file.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is it possible for you to run your process as the desired user via the

<identity impersonate="true" userName="domain\user" password="pass" />

of the web.config file?

If not...

I had to do something like this. The solution recommended to me by an MS newsgroup member was to use WNetAddConnection2A to connect to the share as a different user.  You can then use System.IO.File.Copy to copy the file.  It worked for me.  Here is an example (with error handling omitted for brevity):

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Net;

public class test {

//used in calling WNetAddConnection2
[StructLayout (LayoutKind.Sequential)]
public struct NETRESOURCE
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[MarshalAs (UnmanagedType.LPStr)]
public string lpLocalName;
[MarshalAs (UnmanagedType.LPStr)]
public string lpRemoteName;
[MarshalAs (UnmanagedType.LPStr)]
public string lpComment;
[MarshalAs (UnmanagedType.LPStr)]
public string lpProvider;

//WIN32API - WNetAddConnection2
[DllImport("mpr.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetAddConnection2A (
[MarshalAs (UnmanagedType.LPArray)]
NETRESOURCE [] lpNetResource,
[MarshalAs (UnmanagedType.LPStr)]
string lpPassword,
[MarshalAs (UnmanagedType.LPStr)]
string lpUserName,
int dwFlags

//WIN32API - WNetCancelConnection2
[DllImport("mpr.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetCancelConnection2A (
[MarshalAs (UnmanagedType.LPStr)]
string lpName,
int dwFlags,
int fForce

public int Connect (string share, string user, string pwd)
//The NETRESOURCE struct is required by the WinAPI function
nr[0].lpRemoteName = share;
nr[0].lpLocalName = "";  //mLocalName;
nr[0].dwType = 1;     //disk
nr[0].dwDisplayType = 0;
nr[0].dwScope = 0;
nr[0].dwUsage = 0;
nr[0].lpComment = "";
nr[0].lpProvider = "";
return(WNetAddConnection2A (nr, pwd, user, 0));

public int Disconnect (string share)
return(WNetCancelConnection2A (sShare, 0, -1));

public void CopyFile (string dirFrom, string dirTo, string filename)
Connect (share, "...user", "...password");
File.Copy (dirFrom + "\\" + filename, share + "\\" + filename);
Disconnect (share);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel SakrRounded IT Consultant / Projects manager / Cloud Consultant / IT PlanningCommented:
impersonate and don't use a domain account but create 2 local accounts on both machines with the credentials (password and username) that have each access rights to the according directory

another possible way is to change the aspnet password to one you know and create a local aspnet account on the remote machine with the same password..
Bob LearnedCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by wile_e_coyote

Please leave any comments here within the next seven days.


EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.