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