UNCAccessWithCredentials NetUseAdd method returns 1219 error in .NET C#

Updated on: November 24, 2019

Know the following solution when you are getting 1219 error from NetUseAdd method of UNCAccessWithCredentials class when connecting to remote device or computer:

Using UNCAccessWithCredentials class, it helps to connect remote device or computer, but when the source has already connected to target remote device and at the same time, source computer trying to connect remote device again, then it will give error as "1219", you can check from following command for all existing open remote connections:

net use

The above command will give you list of all open existing remote connections, as shown below:

If the above command lists down your destination remote connection computer name in the lists. 

Solution 1: Use the following command to clear the remote session which is saved on the connecting source computer:

net use \\RemoteComputerName /delete

As shown below you can check we have deleted the remote connection:

Solution 2: From the .NET code, you need to call Dispose() method of UNCAccessWithCredentials class at the end of the method as shown below to delete the remote connection:

  static void Main(string[] args)
        {
            UNCAccessWithCredentials unc = new UNCAccessWithCredentials();
            string ConnectPath = @"\\\\yourentirepath\\";
            string UserName = "username";
            string DomainName = "domainpath.com";
            string Password = "password";
            string Directory = @"\\\\yourentirepath\\";
            if (unc.NetUseWithCredentials(ConnectPath, UserName, DomainName, Password) == true)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(Directory);
                Console.WriteLine("Connected to Folder: " + dirInfo.FullName);
            }
            else
            {
                Console.WriteLine("Not Connected");
            }
            unc.Dispose();
            Console.ReadLine();

        }