title: copy files with authentication in c#
body: <p>I am trying to copy file from local drive to one of folder on server. name of the folder on server is 'DBFiles'. No one has got access to this apart from username 'user' and password 'password1!'</p>
  
  <p>before coping the file, it creates the directory if not exisits as well. </p>
  
  <p>can someone help to get access while creating directory 'Test' and then copy a files. </p>
  
  <pre><code>if (!Directory.Exists(@"\\server-a\copiedfiles\"))
      Directory.CreateDirectory(@"\\server-a\DBFiles\"+Test);   
  File.Copy("C:\Temp\abc.txt", @"\\server-a\DBFiles\");
  </code></pre>
  
  <p>This is the original code in c#.</p>
  
  <pre><code>NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", true); //Disconnect in case we are currently connected with our credentials;
  NetworkShare.ConnectToShare(@"\\server-a\DBFiles", "user1", "password1!"); //Connect with the new credentials
  
  File.Copy(@"c:\temp\T1.txt", @"\\server-a\DBFiles\T1.txt");
  
  NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", false); //Disconnect from the server.
  </code></pre>
  
  <p>Its giving error as Access Denied. </p>