question

RajNair avatar image
0 Votes"
RajNair asked XingyuZhao-MSFT answered

Access a shared folder using the shared machine's local credential

I am trying to access a shared folder "\\machineA\shared", using the credential "machineA\user" from a different machineB. Unfortunately machineA is not in domain as machineB. How can i access the shared folder throgh c#....

tried to use this code not working

NetworkCredential theNetworkCredential = new NetworkCredential(equipment.User, equipment.Password);
CredentialCache theNetCache = new CredentialCache();
theNetCache.Add(new Uri(equipment.SourceFolder), "Basic", theNetworkCredential);
string[] theFiles1 = Directory.GetFiles(equipment.SourceFolder);

dotnet-runtime
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RajeshAmb,

I'm adding [dotnet-runtim] as that is a better place to ask the question.

Best regards,
Tom Jebo
Sr Escalation Engineer
Microsoft Open Specifications Support

0 Votes 0 ·

1 Answer

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered

Hi @RajeshAmb ,
Since machineA is not in domain as machineB, you could use the mpr.dll to add remote network connection.
Here’s an example you may need:

         public static class WNetConnectionHelper
         {
             [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
             private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
    
             [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2"))]
             private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);
    
             [StructLayout(LayoutKind.Sequential)]
             public class NetResource
             {
                 public int dwScope;
                 public int dwType;
                 public int dwDisplayType;
                 public int dwUsage;
                 public string lpLocalName;
                 public string lpRemoteName;
                 public string lpComment;
                 public string lpProvider;
             }
    
             public static uint WNetAddConnection(NetResource netResource, string username, string password)
             {
                 uint result = WNetAddConnection2(netResource, password, username, 0);
                 return result;
             }
    
             public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
             {
                 NetResource netResource = new NetResource();
                 netResource.dwScope = 2;       //RESOURCE_GLOBALNET
                 netResource.dwType = 1;       //RESOURCETYPE_ANY
                 netResource.dwDisplayType = 3; //RESOURCEDISPLAYTYPE_GENERIC
                 netResource.dwUsage = 1;       //RESOURCEUSAGE_CONNECTABLE
                 netResource.lpLocalName = localName;
                 netResource.lpRemoteName = remoteName.TrimEnd('/');
    
                 uint result = WNetAddConnection2(netResource, password, username, 0);
                 return result;
             }
    
             public static uint WNetCancelConnection(string name, uint flags, bool force)
             {
                 uint nret = WNetCancelConnection2(name, flags, force);
                 return nret;
             }
         }

Here is another solution which might be helpful.

Best Regards,
Xingyu Zhao


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.