Automatically Map network drives

Source: Internet
Author: User
Tags sprintf
Automatically map network drives 2010-07-21 13:29:42 Tags: Leisure workplace automatic mapping network drives

1. Using batch files to automatically map ①      first make the automatic Map Network drive batch file;②      The net use command, its basic format is as follows: NET uses [ devicename | *] [\\computername\sharename[\volume] [password | *]]         [/user:[domainname\ ]username]         [/user:[dotted domain Name\]username]          [/user:[username@dotted Domain name]         [ Smartcard]         [/savecred]         [[/ DELETE] | [/persistent:{yes | NO}]] where, devicename is a mapped letter, defined by itself, such as Z:, Y:, etc., if *, then select a letter per creation; ComputerName is the name of the computer you want to map, and you can use its IP address; ShareName to a shared folder ; password is the password required for the connection; username is the user name that connects the computer, savecred whether to save the username and password, persistent whether to permanently connect, yes or no, as follows: @echo off net use Z: \\Server\ sharefile  "" "/USER:" Guest "/persistent:no map \\Server\sharefile to Z: disk, user name is guest, password is empty ③       Production steps are as follows: First define a TXT file, and then modify its extension to bat, such as Netmapping.bat;This batch file is written with the following command: @echo off net use Z: \\server\sharefile  ""/user: "Guest"/persistent:no modify the name of the computer you want to connect to, share the file name, Login username and password, as well as a defined drive letter. Then there are several ways to do it, the easiest way is to put the batch file in the Startup key, and the user will automatically map the network drive every time they log off or restart the computer, but if z: The mappings already exist, they are no longer created.   2.        uses API functions to automatically map network drives First contains the required Lib library and header files, that is, the Mpr.lib and #i nclude <winnetwk.h>①      determine if the mapped network drive path exists and needs to include #i Nclude & lt;io.h>          Cstring strtempath = _t ("Z:");          if (_access (strtempath,0)!= 0)//no This folder           {                    MessageBox ("Z: not present!");         }②      If present, disconnect it first           char remotepath[256];          cstring strremote = _t ("\\\\server\\sharefile");          Dword len = Strremote.getlength ();          sprintf (RemotePath, Strremote,len);         //First remove network connection          dword retcancel = WNetCancelConnection2 (remotepath,0,true); Forcibly disconnect          if (retcancel!= no_error)           {                    MessageBox ("Disconnect Network Map failed!");         }③      automatically create mapped network drives netresource  NETR;                    netr.dwscope   =   resource_globalnet;                       netr.dwtype   =   resourcetype_disk;                        netr.dwdisplaytype   =&NBSP;&NBsp resourcedisplaytype_share;                       netr.dwusage   =   resourceusage_connectable;                    netr.lplocalname   =  "Z:";  //letter can be modified                     netr.lpremotename =  "\\\\ server\\ Sharefile  //the shared file to map                     netr.lpcomment   =   "Automatically create mapped network drives";                    netr.lpprovider   =   ""; Dword ret = WNetAddConnection2 (&netr, "", "Guest", NULL);                    if (ret = error_network_unreachable)                     {                              MessageBox ("Network not!");                   }                    if (ret = error_already_assigned)                     {                             MessageBox ("This network map already exists!");                   }                     if (no_error!= ret)                     {                              MessageBox ("Automatic mapping does not succeed!");                   }  3. Solve the problem of automatic disconnection of mapped network driveThe mapped network drive is automatically disconnected for a period of time due to the default time-out period for the Server service's automatic disconnect feature, and we can change the disconnect time in the following ways. ⑴ use Registry Editor to increase the default time-out period. A, locate and click the following registry key in the registry: Hkey_local_machine\system\currentcontrolset\services\lanmanserver\parameters B, in the right pane, click the value Autodisconnect, and then on the Edit menu, click Modify. If the value autodisconnect does not exist, follow these steps: On the Edit menu, point to New, and then click REG_DWORD. C type Autodisconnect, and then press ENTER. D, click Hex.   Type ffffffff in the Value data box, and then click OK. ⑵ Change the default time-out period A, open a command window, type the net Config server/autodisconnect:number command line, and then press ENTER, where number is the minutes that you want the server to wait before disconnecting the mapped network drive. The maximum value for this command is 65535. B, if the automatic disconnect value is set to 0, does not mean that the automatic disconnect feature is turned off, but rather that the server service disconnects the mapped network drive only after a few seconds of inactivity. C, to turn off the automatic disconnect feature, open the Command window, type the net Config server/autodisconnect:-1 command, and then press ENTER 4. Gets the information for the mapped network connection char remotepath[256]; CString strlocal = _t ("Z:");
Char local[10];
sprintf (Local,strlocal, Strlocal.getlength ()); DWORD Remotelen = 256;
dword  retget = wnetgetconnection (Strlocal,remotepath,&remotelen); Where RemotePath returns the specified shared connection path.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.