Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to assigning a Drive to a Share ?

how to assigning a Drive to a Share ?

Scheduled Pinned Locked Moved C#
helpcsharpjsontutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zoltix
    wrote on last edited by
    #1

    how to assigning a Drive to a Share in c#? Currently, i try the API for make that but he don't run.... Could you help me ? 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 }; public class ServerEnum : IEnumerable { enum ErrorCodes { NO_ERROR = 0, ERROR_NO_MORE_ITEMS = 259 }; public class ServerEnum : IEnumerable { enum ErrorCodes { NO_ERROR = 0, ERROR_NO_MORE_ITEMS = 259 }; [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", EntryPoint="WNetAddConnection2", CallingConvention=CallingConvention.Winapi)] private static extern ErrorCodes WNetAddConnection2(NETRESOURCE lpNetResource,ref string lpPassword,ref string lpUsername, System.UInt32 dwFlags ); public void connectMapDrive(){ NETRESOURCE lpNetResource = new NETRESOURCE(); lpNetResource.dwType =ResourceType.RESOURCETYPE_ANY; lpNetResource.lpLocalName="O:"; lpNetResource.lpRemoteName= @"\\Bruxptest\Fichiers" ; lpNetResource.lpProvider = ""; string lpPassword ="PASSWORD**" ; string lpLocalName =@"bruxptest\ll2"; Erro

    H 1 Reply Last reply
    0
    • Z zoltix

      how to assigning a Drive to a Share in c#? Currently, i try the API for make that but he don't run.... Could you help me ? 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 }; public class ServerEnum : IEnumerable { enum ErrorCodes { NO_ERROR = 0, ERROR_NO_MORE_ITEMS = 259 }; public class ServerEnum : IEnumerable { enum ErrorCodes { NO_ERROR = 0, ERROR_NO_MORE_ITEMS = 259 }; [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", EntryPoint="WNetAddConnection2", CallingConvention=CallingConvention.Winapi)] private static extern ErrorCodes WNetAddConnection2(NETRESOURCE lpNetResource,ref string lpPassword,ref string lpUsername, System.UInt32 dwFlags ); public void connectMapDrive(){ NETRESOURCE lpNetResource = new NETRESOURCE(); lpNetResource.dwType =ResourceType.RESOURCETYPE_ANY; lpNetResource.lpLocalName="O:"; lpNetResource.lpRemoteName= @"\\Bruxptest\Fichiers" ; lpNetResource.lpProvider = ""; string lpPassword ="PASSWORD**" ; string lpLocalName =@"bruxptest\ll2"; Erro

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      First, you didn't describe any errors or what exactly the problem was. It's very hard to help you when you don't mention specific problems. Second, why not make this easier:

      string drive = "X"; // Or whatever
      string share = @"\\machine\share"; // Or whatever
      Process.Start("net.exe", string.Format("use {0}: {1}", drive, share));

      You could easily add switches for a username and password, too. Type "net use /?" in the command prompt for more information. If you'd rather do this programmatically, then please be more specific about the nature of your problem.

      Microsoft MVP, Visual C# My Articles

      Z 1 Reply Last reply
      0
      • H Heath Stewart

        First, you didn't describe any errors or what exactly the problem was. It's very hard to help you when you don't mention specific problems. Second, why not make this easier:

        string drive = "X"; // Or whatever
        string share = @"\\machine\share"; // Or whatever
        Process.Start("net.exe", string.Format("use {0}: {1}", drive, share));

        You could easily add switches for a username and password, too. Type "net use /?" in the command prompt for more information. If you'd rather do this programmatically, then please be more specific about the nature of your problem.

        Microsoft MVP, Visual C# My Articles

        Z Offline
        Z Offline
        zoltix
        wrote on last edited by
        #3

        sorry, I would like to make a windows service which able to check and map the shared drive automatically. When i launch my api for that, I receive a error message 2202: ERROR_INVALID_WINDOW_STYLE the equivalence in c++ is : #include #include #include void main() { NETRESOURCE nr; DWORD res; TCHAR szUserName[32] = "MyUserName", szPassword[32] = "MyPassword", szLocalName[32] = "Q:", szRemoteName[MAX_PATH] = "\\\\products2\\relsys"; // // Assign values to the NETRESOURCE structure. // nr.dwType = RESOURCETYPE_ANY; nr.lpLocalName = szLocalName; nr.lpRemoteName = szRemoteName; nr.lpProvider = NULL; // // Call the WNetAddConnection2 function to assign // a drive letter to the share. // res = WNetAddConnection2(&nr, szPassword, szUserName, FALSE); // // If the call succeeds, inform the user; otherwise, // print the error. // if(res == NO_ERROR) printf("Connection added \n", szRemoteName); else printf("Error: %ld\n", res); return; } and it is work? -=zoltx=-

        D 1 Reply Last reply
        0
        • Z zoltix

          sorry, I would like to make a windows service which able to check and map the shared drive automatically. When i launch my api for that, I receive a error message 2202: ERROR_INVALID_WINDOW_STYLE the equivalence in c++ is : #include #include #include void main() { NETRESOURCE nr; DWORD res; TCHAR szUserName[32] = "MyUserName", szPassword[32] = "MyPassword", szLocalName[32] = "Q:", szRemoteName[MAX_PATH] = "\\\\products2\\relsys"; // // Assign values to the NETRESOURCE structure. // nr.dwType = RESOURCETYPE_ANY; nr.lpLocalName = szLocalName; nr.lpRemoteName = szRemoteName; nr.lpProvider = NULL; // // Call the WNetAddConnection2 function to assign // a drive letter to the share. // res = WNetAddConnection2(&nr, szPassword, szUserName, FALSE); // // If the call succeeds, inform the user; otherwise, // print the error. // if(res == NO_ERROR) printf("Connection added \n", szRemoteName); else printf("Error: %ld\n", res); return; } and it is work? -=zoltx=-

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Are you running the app as the LOCAL SYSTEM account or as a user? Also, you have to tag the service as able to interact with the desktop. RageInTheMachine9532

          Z 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Are you running the app as the LOCAL SYSTEM account or as a user? Also, you have to tag the service as able to interact with the desktop. RageInTheMachine9532

            Z Offline
            Z Offline
            zoltix
            wrote on last edited by
            #5

            user, and interact........ -=zoltx=-

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups