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());
}
}
}
加入以下namespace:{
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());
}
}
}
using System.Diagnostics;
还可以通过using System.Management;实现具体代码还未完成
-------------------------------------------------------------------------------------------------
以上方法做成服务在本机运行正常,可放在服务器怎么也执行不了,没发了,换招.
用WNetAddConnection2函数
using System.Runtime.InteropServices;
[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
public static extern uint WNetAddConnection2([In] NETRESOURCE lpNetResource,string lpPassword,string lpUsername,uint dwFlags);
[DllImport("Mpr.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;
}
public static extern uint WNetAddConnection2([In] NETRESOURCE lpNetResource,string lpPassword,string lpUsername,uint dwFlags);
[DllImport("Mpr.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;
}
調用方法:
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);
映射为本机,然后再操作,完成后删除.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);
参考:愚翁专栏