copy files with authentication in c#

前端 未结 4 1972
礼貌的吻别
礼貌的吻别 2020-12-01 09:03

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\'

4条回答
  •  青春惊慌失措
    2020-12-01 09:25

    NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", true); //Remove this line
    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); //Remove this line also
    

    After 02 days search in Google finally the above worked for me. If you use 'NetworkShare.DisconnectFromShare' after the first login you will get an error as 'Access denied'. Then every time you need to restart the server or need to execute 'net use * /del' command to delete the saved credentials in windows.

提交回复
热议问题