Copy files in the specified folder in the LAN according to the user name/Password

Source: Internet
Author: User
The Code is as follows: public static void opendirectory (string str_directory, string to_directory, string str_username, string str_password)
{
String str_logoon = "net use" + str_directory + "/User:" + str_username + "\" "+ str_password + "\"";
String str_opendirectory = "start" + str_directory;

Process p_tmp = new process ();
P_tmp.startinfo.redirectstandarderror = true;
P_tmp.startinfo.redirectstandardinput = true;
P_tmp.startinfo.redirectstandardoutput = true;
P_tmp.startinfo.useshellexecute = false;
P_tmp.startinfo.createnowindow = true;
P_tmp.startinfo.filename = "cmd.exe ";
P_tmp.start ();
P_tmp.standardinput.writeline (str_logoon );
P_tmp.standardinput.writeline (str_opendirectory );
P_tmp.standardinput.writeline ("exit ");
P_tmp.close ();
P_tmp.dispose ();

String [] files = directory. getfiles (fromdirectory );
If (files. length> 0)
{
Foreach (string s in files)
{
Fileinfo objfi = new system. Io. fileinfo (s );
File. Copy (str_directory + "\" + objfi. Name. tostring (), to_directory + "\" + objfi. Name. tostring ());
}
}
}

Add the following namespace:
Using system. diagnostics;
You can also use using system. management; To implement specific code that has not been completed.
Bytes -------------------------------------------------------------------------------------------------
The above method makes the service run normally on the local machine, but it cannot be executed on the server. If it is not done, change the trick.
Use the wnetaddconnection2 Function
Using system. runtime. interopservices; [dllimport ("MIP. dll", entrypoint = "wnetaddconnection2")]
Public static extern uint wnetaddconnection2 ([in] netresource lpnetresource, string lppassword, string lpusername, uint dwflags );

[Dllimport ("MIP. dll")]
Public 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 localname;

Public String remotename;

Public String comment;

Public String provider;
}

Usage method: netresource mynetresource = new netresource ();
Mynetresource. dwscope = 2; // 2: resource_globalnet
Mynetresource. dwtype = 1; // 1: resourcetype_any
Mynetresource. dwdisplaytype = 3; // 3: resourcedisplaytype_generic
Mynetresource. dwusage = 1; // 1: resourceusage_connectable
Mynetresource. localname = "T :";
Mynetresource. remotename = @ "\ 172.16.20.12 \ e $ \ ccl_development \ proexceltest \ engg_index_dfs \ Report ";
Mynetresource. provider = NULL;
Wnetaddconnection2 (mynetresource, "jinseyd00", "cclsjpdc1 \ wusf", 0 );
Wnetcancelconnection2 ("T:", 1, true );

Map to the local machine, and then perform the operation. After the operation is completed, delete it.
Reference: yuweng Column

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.