title: Copying files from one PC To another without sharing the destination folder with everyone in C#
body: <p>I want to transfer a folder with data from one PC to another .However the destination PC is not shared with everyone .A specific user has been given read/write access to it .I tried impersonation of the user and tried to transfer ,however i get the error &quot;network name not found&quot; .If i share the folder with everyone i am able to copy the folder data from source to destination.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Runtime.InteropServices;
  using System.Security.Principal;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace VMS3.Admin.Forms_Deployment
  {
      /// &lt;summary&gt;
      /// Interaction logic for fDeploy.xaml
      /// &lt;/summary&gt;
      public partial class fDeploy : Page
      {
          public fDeploy()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              
              
              string sourcePath = txtFileupload.Text;
              string targetPath = &quot;\\\\server name\\Application\\VMS3&quot;;
              
  
  
              if (ImpersonateUser(&quot;BRANCHOFFICE&quot;, &quot;Username&quot;, &quot;Password&quot;) == true)
              {
                  
                  CopyFilesRecursively(sourcePath, targetPath);
  
                  undoImpersonation();
              }
              else
              {
                  //Could not authenticate account. Something is up.
                  //Log or something.
              }
  
          }
  
          #region Impersionation global variables
          public const int LOGON32_LOGON_INTERACTIVE = 2;
          public const int LOGON32_PROVIDER_DEFAULT = 0;
  
          WindowsImpersonationContext impersonationContext;
  
          [DllImport(&quot;advapi32.dll&quot;)]
          public static extern int LogonUserA(String lpszUserName,
              String lpszDomain,
              String lpszPassword,
              int dwLogonType,
              int dwLogonProvider,
              ref IntPtr phToken);
          [DllImport(&quot;advapi32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true)]
          public static extern int DuplicateToken(IntPtr hToken,
              int impersonationLevel,
              ref IntPtr hNewToken);
  
          [DllImport(&quot;advapi32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true)]
          public static extern bool RevertToSelf();
  
          [DllImport(&quot;kernel32.dll&quot;, CharSet = CharSet.Auto)]
          public static extern bool CloseHandle(IntPtr handle);
          #endregion
  
          private bool ImpersonateUser(string domain, string userName, 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;
          }
  
          /// &lt;summary&gt;
          /// Undoes the current impersonation.
          /// &lt;/summary&gt;
          private void undoImpersonation()
          {
              impersonationContext.Undo();
          }
  
  
          private static void CopyFilesRecursively(string sourcePath, string targetPath)
          {
              try
              {
                  foreach (string dirPath in Directory.GetDirectories(sourcePath, &quot;*&quot;, SearchOption.AllDirectories))
                  {
                      Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
                  }
  
                  //Copy all the files &amp; Replaces any files with the same name
                  foreach (string newPath in Directory.GetFiles(sourcePath, &quot;*.*&quot;, SearchOption.AllDirectories))
                  {
                      File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
                  }
                  MessageBox.Show(&quot;File copied successfully&quot;);
              }catch(Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
              //Now Create all of the directories
              
          }
  
      
      }
      
  }
  </code></pre>