使用WMI
using System;
using System.Management;class Sample_ConnectionOptions
{
    public static int Main(string[] args) {
        ConnectionOptions options = new ConnectionOptions();
        options.Username = 用户名; //could be in domain\user format
        options.Password = 密码;
        ManagementScope scope = new ManagementScope(
            "\\\\servername\\root\\cimv2",
            options);
        try {
            scope.Connect();
            ManagementObject disk = new ManagementObject(
                scope,
                new ManagementPath("Win32_logicaldisk='c:'"),
                null);
            disk.Get();
            //接着就可以拷文件了,就象在本地操作一样,不过要加上网络路径
            File.Copy(@"\\remotepc\c:\te.txt","c:\\te1.txt");
        }
        catch (Exception e) {
            Console.WriteLine("Failed to connect: " + e.Message);
        }
        return 0;
    }
}

解决方案 »

  1.   

    使用wmi出现一些问题。直接运行该程序时,File.Copy(@"\\remotepc\c:\te.txt","c:\\te1.txt");报错,主要因为格式不支持,改为File.Copy(@"\\remotepc\c$\te.txt","c:\\te1.txt");访问隐藏共享c盘。如果在之前一段时间,使用了windows explorer访问过远端机器c盘,那么运行成功,即使没有之前的wmi代码;如果第一次访问该远端机器,就算包含wmi代码,总是报错:文件找不到。
      

  2.   

    using System;
    //using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    //using System.Windows.Forms;
    using System.IO;
    using System.Threading;
    using System.Runtime.InteropServices;
    using fd;
    //using ShellLib;public class RemoteConnect 
    {
    #region NetDll
    [DllImport("Mpr", SetLastError=true)]
    public static extern uint WNetAddConnection(string lpRemoteName, string lpPassword, string lpLocalName);
    [DllImport("Mpr", SetLastError=true)]
    public static extern uint WNetCancelConnection(string lpRemoteName, bool fForce);
    [DllImport("kernel32")]
    public static extern uint WinExec(string command,uint isshow);
    #endregion
    public static void Main() 
    {
    string lpRemoteName = "administrator";
    string lpPassword   = "dsf"; 
    string lpLocalName  = "192.168.108.1"; SetTime setTime = new SetTime(lpLocalName,lpRemoteName,lpPassword);
    uint dwResult = WNetCancelConnection(lpRemoteName,true);
    if (dwResult != 0)
    {

    }
                                              
    dwResult = WNetAddConnection(lpRemoteName, lpPassword, lpLocalName);
    if (dwResult != 0)
    {

    }

    string strFlPath = @"\\192.168.108.11\dbf\1.txt";
    File.CreateText(strFlPath+"dduyuygfdsgfds"); }
    }
    using System;
    using System.Runtime.InteropServices;
    namespace fd
    {
    /// <summary>
    /// SetTime 的摘要说明。
    /// </summary>
    public class SetTime
    {
    [DllImport("kernel32")] private static extern uint WinExec(string command,uint isshow); public SetTime(string servername,string strUser,string strPwd)
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    string strCommnad = null;
    if (strUser == null || strUser == "")
    {
    strCommnad = "net use \\\\" + servername + " " + strPwd + " /user:Administrator" + " /y";
    }
    else
    {
    strCommnad = "net use \\\\" + servername + " " + strPwd + " /user:" + strUser + " /y";
    }

    //System.Windows.Forms.MessageBox.Show(strCommnad);
    WinExec(strCommnad,0);
    //WinExec("net time \\\\"+servername+" /set /y",0);
    WinExec("net time \\\\"+servername+" /set /y",0);
    }
    }
    }存放文件的文件夹必须是共共享