Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file on a network shared drive

Tags:

I have a network shared drive ("\serveur\folder") on which I would like to copy file. I can write on the drive with a specific user ("user"/"pass"). How can I access the shared drived with write privilege using C#?

like image 644
Martin Delille Avatar asked Sep 16 '09 10:09

Martin Delille


People also ask

How do I copy files from a network drive?

Navigate to the file or folder that needs to be transferred and right click on it. This will bring up a context menu, click on Copy. Go back to This PC and open the G: drive. From there you can select either My Drive (personal drive) or Shared Drives for folders that have been shared to your account.

How do I copy files to a shared drive?

To move files from My Drive into a shared drive or between shared drives, drag the files into the destination shared drive.

How do I copy files from a shared folder?

Go the shared folder, which contains files you want to copy to your drive. Select all the files you want to copy. In the upper right corner click on three vertical dots and select “make a copy” The files will then appear in your drive.

How do I copy files from network drive to local drive automatically?

Click "Schedule Sync" at the bottom and enable it, then select any schedule sync you like. Then, click “Start Sync” to sync folder to network drive. You can select schedule settings based on different frequencies, specific events or USB drive ( to automatically sync files between computer and flash drive).


1 Answers

Untested code, but it will be similiar to:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);  // http://pinvoke.net/default.aspx/advapi32/LogonUser.html     IntPtr token; LogonUser("username", "domain", "password", LogonType.LOGON32_LOGON_BATCH, LogonProvider.LOGON32_PROVIDER_DEFAULT);  WindowsIdentity identity = new WindowsIdentity(token);  WindowsImpersonationContext context = identity.Impersonate();  try {     File.Copy(@"c:\temp\MyFile.txt", @"\\server\folder\Myfile.txt", true); } finally {     context.Undo(); } 
like image 97
Mitch Wheat Avatar answered Sep 25 '22 12:09

Mitch Wheat