1,调用API的方法WNetAddConnection2实现磁盘映射,这样就可以像操作本地磁盘一样获取有用的文件
using System.IO;
using System.Runtime.InteropServices;
[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;
}
[DllImport("mpr.dll",CharSet = CharSet.Ansi)]
public static extern int WNetAddConnection2( NetResource netResource, String password, String Username, int Flag);
[DllImport("mpr.dll", CharSet = CharSet.Ansi)]
public static extern int WNetCancelConnection2(String localname, int flag, bool ss);
static void Main(string[] args)
{
//用户密码
string password = System.Configuration.ConfigurationSettings.AppSettings["password"];
//用户名
string userName = System.Configuration.ConfigurationSettings.AppSettings["userName"];
NetResource myNetResource = new NetResource();
myNetResource.dwScope = 2;//RESOURCE_GLOBALNET
myNetResource.dwType = 1 ;//RESOURCETYPE_DISK
myNetResource.dwDisplayType = 3;//RESOURCEDISPLAYTYPE_SHARE
myNetResource.dwUsage = 1;//RESOURCEUSAGE_CONNECTABLE
myNetResource.LocalName = "w:";
myNetResource.RemoteName = remotePath;
myNetResource.Provider = null;
//建立与远程机器的连接
int ret = WNetAddConnection2(myNetResource, password, userName, 0);
//复制文件
File.Copy("w://ss.txt", "C://Documents and Settings//All Users//Documents//ss.txt", true);
//关闭连接
int retdel = WNetCancelConnection2(myNetResource.LocalName, 0, true);
}