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

12
  • 1
    And you've confirmed that the URI is valid by connecting to the share via an independent client? Commented May 14, 2023 at 16:23
  • @RTF Yes by connecting to "\\192.168.1.89\Main-Storage\" using run command on windows Commented May 14, 2023 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. Commented May 14, 2023 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); }" Commented May 14, 2023 at 20:42
  • stackoverflow.com/a/1197430/12372795 Commented May 14, 2023 at 20:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's worth including the solution in the answer, I think: in my case I replaced [DllImport("mpr.dll"] with [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2", SetLastError = true, CharSet = CharSet.Auto)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.