Search code examples
c#ionetworkcredentialswnet

How to resolve this error "ERROR_BAD_USERNAME" in WNetAddConnection2?


My problem is to copy file from one system to another system on the same network. I googled for the above problem, I started working with Mark Brackett's answer.

Now i am planning to copy the files from my system to another system, but i am getting following error codes 53, 67, 2202. I resolved the first two error codes successfully. Now i am stuck with 2202. please look into the below code.

NetworkCredential credentials = new NetworkCredential(@"\\192.168.0.110\", "krishna4");

private void btnClick_Click(object sender, EventArgs e)
    {
// NetwrokConnection(string networkName, NetworkCredential credentials)
        using (new NetworkConnection("\\\\10.235.115.210\\d", credentials)) ; 
        System.IO.File.Copy("D:\\English\\parts.oxps", @"\\192.168.0.110\test\parts.oxps", true);
    }

Please help to resolve the problem. Thanks in advance.


Solution

  • I think your connection credentials may be wrong.it may be invalid user name.

    I suggest to refer following link:

    ERROR_BAD_USERNAME

    WNetUseConnection2 in C#

    How to finish this implementation of creating a network share using WNetAddConnection2?

    ERROR_BAD_USERNAME :The specified user name is not valid.

    Here is Sample Code;

    using System;
    using System.IO;
    using System.Net;    
    
        class Program
        {
            static void Main(string[] args)
            {
              var networkPath = @"//server/share";
              var credentials = new NetworkCredential("username", "password");
    
              using (new NetworkConnection(networkPath, credentials))
              {
                var fileList = Directory.GetFiles(networkPath);
              }
    
              foreach (var file in fileList)
              {
                  Console.WriteLine("{0}", Path.GetFileName(file));
              }          
            }
        }