RoboCopy from Local Machine to Fileshare using Process.Start() throws access denied error
<p>I have network fileshare.The network share has login account where the user will be autheticated whenever they copy files to the directory.First of all user will create the directory in file share using <code>WindowsImpersonationContext</code> in c#.After creating directory user is coppying the file to the directory in the networkshare.</p>

<p>User is able to create directory in the network share but when the user starts robocopy by copying files from local machine to network file share, system says <code>Access denied</code>.I am using same user account for impersonating the to fileshare account while creating directory in the fileshare as well as robocopy.</p>

<p>please help me. please find the below code. 
is it possible to run robocopy as other account or impersonate other user account while copying file to network fileshare directory.</p>

<h2>create Directory in Network Fileshare</h2>

<pre><code>public void CreateFolder(string FilePath,string UserName,string DomainName,string Password)
   {
       try
       {

           string path = FilePath;
           bool impersonateResult=impersonateValidUser(UserName, DomainName, Password);
           if (impersonateResult)
           {
               Directory.CreateDirectory(path);
           }

       }
       catch (Exception ex)
       {

           throw ex;  
       }
   }
</code></pre>

<h2>Impersonation while creating directory in Filshare</h2>

<pre><code> public bool impersonateValidUser(String userName, String domain, String password)
   {
       WindowsIdentity tempWindowsIdentity;
       IntPtr token = IntPtr.Zero;
       IntPtr tokenDuplicate = IntPtr.Zero;

       if (RevertToSelf())
       {
           if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
               LOGON32_PROVIDER_DEFAULT, ref token) != 0)
           {
               if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
               {
                   tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                   impersonationContext = tempWindowsIdentity.Impersonate();
                   if (impersonationContext != null)
                   {
                       CloseHandle(token);
                       CloseHandle(tokenDuplicate);
                       return true;
                   }
               }
           }
       }
       if (token != IntPtr.Zero)
           CloseHandle(token);
       if (tokenDuplicate != IntPtr.Zero)
           CloseHandle(tokenDuplicate);
       return false;
   }
</code></pre>

<h2>Robocopy:</h2>

<pre><code>public void RoboCopy(string strSourceFilePath,string DestinationFilePath,string FileName,string UserName,String DomainName,string Password)
   {
       try
       {


           System.Security.SecureString password = new System.Security.SecureString();
           char[] pass = Password.ToCharArray();
           foreach (char c in pass)
            password.AppendChar(c);
            Process p = new Process();
           p.StartInfo = new ProcessStartInfo();
           p.StartInfo.FileName = "Robocopy.exe";
           p.StartInfo.Arguments = string.Format("\"{0}\"  \"{1}\"  \"{2}\"  \"{3}\"   ", strSourceFilePath, DestinationFilePath, FileName,"/R:2" );
           p.StartInfo.Domain = DomainName;
           p.StartInfo.UserName = UserName;
           p.StartInfo.Password = password;       
           p.StartInfo.UseShellExecute = false; // set this to false so that we can redirect the output
           p.StartInfo.RedirectStandardError = true; 
           p.StartInfo.RedirectStandardOutput = true;
           p.Start();
           string result = p.StandardOutput.ReadToEnd();
           string output = p.StandardOutput.ReadToEnd();
           p.WaitForExit();



       }
       catch (Exception ex)
       {
           // Setting the Error Description in CommonFunctions Class.
             throw ex; 
       }
   }
</code></pre>

<p>While doing robocopy only I am getting access denied error when I copy file from local machine to Network Filesahre using Fileshare Login account.
I am able to create directory without any access denied error?</p>
