0

I am doing a programmatically access of a SMB Network Shared Drive using C#

I saw this thread How to provide user name and password when connecting to a network share which let's you define UNC path and some credentials.

I've used similar logic on giving the credentials on accessing the share like Network Share Gist but they both give an error 67 which is "The network name cannot be found". Even though the network share is mapped as network drive, the error still persists.

I am now hitting the wall to make work as almost all of the thread I've seen is not working for me.

This is my code accessing the network share using the class that is advised on the thread above.

NetworkCredential credentials = new NetworkCredential("user", "pass");
using (new smbconn(@"\\192.168.1.89\Main-Storage\", credentials))
{
   Files = Directory.GetFiles(@"\\192.168.1.89\Main-Storage\");
   MessageBox.Show(string.Join(Environment.NewLine, Files));
}

If more additional information is needed, please don't hesitate to comment. Thanks

CSAPawn
  • 47
  • 6
  • 1
    And you've confirmed that the URI is valid by connecting to the share via an independent client? – RTF May 14 '23 at 16:23
  • @RTF Yes by connecting to "\\192.168.1.89\Main-Storage\" using run command on windows – CSAPawn May 14 '23 at 20:32
  • Have you tried temporarily commenting out the code inside the using block? Just curious if the creation of the connection itself is causing the error or the fetching of files. – RTF May 14 '23 at 20:40
  • The error is coming out from the class "sbconn" which I got the code from the stackoverflow thread that I link above. Mainly here "if (result != 0) { throw new Win32Exception(result); }" – CSAPawn May 14 '23 at 20:42
  • https://stackoverflow.com/a/1197430/12372795 – CSAPawn May 14 '23 at 20:45
  • I see. Unfortunately I'm not an expert on this so there's little more I can say. I would be curious if Powershell has any issue connecting, something like New-SmbMapping -RemotePath '\\server' -Username "domain\username" -Password "password" since it uses .Net objects. – RTF May 14 '23 at 20:52
  • @RTF I tried your command and it looks like it gets connected successfully https://i.stack.imgur.com/WVEdn.png – CSAPawn May 14 '23 at 21:09
  • What is the nature of this "smbconn" class? I don't find a .Net class by that name when googling. – RTF May 14 '23 at 21:12
  • Check the stackoverflow answer that I link above. I just renamed the class from "NetworkConnection" to smbconn – CSAPawn May 14 '23 at 21:13
  • That's quite an old thread. Are you attempting to use this code in a Core or Framework project? – RTF May 14 '23 at 21:17
  • Framework project – CSAPawn May 14 '23 at 21:22
  • Looks like I have the solution now, posting it in a minute – CSAPawn May 14 '23 at 21:29

1 Answers1

0

I stumbled upon a really newer thread and has the same type of error with this.

Anders Finn Jørgensen Asnwer

It comes down on how the passed variables like the network path and credentials is being passed to the parameters of WNetAddConnection2.

It completely fixes the problem.

CSAPawn
  • 47
  • 6