none
how do i re-use the existing shared resource connection in c#? RRS feed

  • Question

  • Hi,

    Please help me !!!!!

    In my application, i am establishing connection to the shared resource using WNetAddConnection2 () method from windows network API , also i am Listing all the existing shared resources using WNetEnumResource ()  method from windows network API.

    Example : "172.28.xxx.xxx\Test" is accessed from the PC with valid credentials. PC is remembered the connection till it restarts.

    Both the API methods are accessing using Pinvoke mechanism.

    I want to retrieve the folders from "172.29.xxx.xx". Before retrieving the folders i am checking the connection to the folder is already exists or not.

    How do i re-use the existing shared resource connection to get the folders in c#?
    Which API method needs to call get the connection?


    Tuesday, December 24, 2019 5:24 AM

All replies

  • Hi DeepakHirapur,

    Thank you for posting here.

    For your question, you want to use C # to detect the existence of the connection created by C++.

    You can try the following code.

     public class NetworkDrive
        {
            public enum ResourceScope
            {
                RESOURCE_CONNECTED = 1,
                RESOURCE_GLOBALNET,
                RESOURCE_REMEMBERED,
                RESOURCE_RECENT,
                RESOURCE_CONTEXT
            }
    
            public enum ResourceType
            {
                RESOURCETYPE_ANY,
                RESOURCETYPE_DISK,
                RESOURCETYPE_PRINT,
                RESOURCETYPE_RESERVED
            }
    
            public enum ResourceUsage
            {
                RESOURCEUSAGE_CONNECTABLE = 0x00000001,
                RESOURCEUSAGE_CONTAINER = 0x00000002,
                RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
                RESOURCEUSAGE_SIBLING = 0x00000008,
                RESOURCEUSAGE_ATTACHED = 0x00000010,
                RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
            }
    
            public enum ResourceDisplayType
            {
                RESOURCEDISPLAYTYPE_GENERIC,
                RESOURCEDISPLAYTYPE_DOMAIN,
                RESOURCEDISPLAYTYPE_SERVER,
                RESOURCEDISPLAYTYPE_SHARE,
                RESOURCEDISPLAYTYPE_FILE,
                RESOURCEDISPLAYTYPE_GROUP,
                RESOURCEDISPLAYTYPE_NETWORK,
                RESOURCEDISPLAYTYPE_ROOT,
                RESOURCEDISPLAYTYPE_SHAREADMIN,
                RESOURCEDISPLAYTYPE_DIRECTORY,
                RESOURCEDISPLAYTYPE_TREE,
                RESOURCEDISPLAYTYPE_NDSCONTAINER
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private class NETRESOURCE
            {
                public ResourceScope dwScope = 0;
                public ResourceType dwType = 0;
                public ResourceDisplayType dwDisplayType = 0;
                public ResourceUsage dwUsage = 0;
                public string lpLocalName = null;
                public string lpRemoteName = null;
                public string lpComment = null;
                public string lpProvider = null;
            }
            [DllImport("mpr.dll")]
            private static extern int WNetAddConnection2(NETRESOURCE lpNetResource, string lpPassword, string lpUsername, int dwFlags);
            public int MapNetworkDrive(string unc, string drive, string user, string password)
            {
                NETRESOURCE myNetResource = new NETRESOURCE();
                myNetResource.lpLocalName = drive;
                myNetResource.lpRemoteName = unc;
                myNetResource.lpProvider = null;
                int result = WNetAddConnection2(myNetResource, password, user, 0);
                return result;
            }
        }

    Then use it like this.

            static void Main(string[] args)
            { 
                NetworkDrive nd = new NetworkDrive();
                if (nd.MapNetworkDrive(@"\\servername\shardrive", "Z:", null, null) == 0)
                {
                    Console.WriteLine("Mapped!");
                }
                else
                {
                    Console.WriteLine("Failed!");
                }
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, December 25, 2019 7:34 AM
  • Hi Timon Yang,

    I want to reuse the connection.

    Thursday, December 26, 2019 3:47 AM
  • You can simply use the standard IO calls at that point. When you call WNetAddConnection2 you're creating a mapped drive. So you might say that `\\server\path` maps to `N:`. This is identical to if the user had used Windows Explorer to map a network drive manually.

    From this point on you can use the drive letter just like you would any other path.

    var files = Directory.GetFiles(@"N:\", "*.*");

    You don't need to do anything with P/Invoke unless you want to add/update/remove the mapped drive itself.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, December 26, 2019 4:25 PM
    Moderator