try
{
string[] SubDir=Directory.GetDirectories("\\computername")
}
catch (exception e)
{
errMsg=e.Message
}以上代码运行时出现错误为:“登录失败: 未知的用户名或错误密码。”
请问怎样提供用户名和密码,以连接到指定的计算机?
例如,在XP的开始-》运行对话框中输入“\\ComputerName”,点击确定后会出现一个输入用户名和密码的对话框。但是在程序中怎么实现连接到计算机呢?

解决方案 »

  1.   

    下面是sdk中的例子。
    先使用Wmi开通连接,再Copy文件
    using System;
    using System.Management;// This example demonstrates how to connect to remote machine
    // using supplied credentials.
    class Sample_ConnectionOptions
    {
        public static int Main(string[] args) {
            ConnectionOptions options = new ConnectionOptions();
            options.Username = UserName; //could be in domain\user format
            options.Password = SecurelyStoredPassword;
            ManagementScope scope = new ManagementScope(
                "\\\\servername\\root\\cimv2",
                options);
            try {
                scope.Connect();
                ManagementObject disk = new ManagementObject(
                    scope,
                    new ManagementPath("Win32_logicaldisk='c:'"),
                    null);
                disk.Get();
            }
            catch (Exception e) {
                Console.WriteLine("Failed to connect: " + e.Message);
            }
            return 0;
        }
    }
      

  2.   

    ConnectionOptions options = new ConnectionOptions();
            options.Username = UserName; //could be in domain\user format
            options.Password = SecurelyStoredPassword;
            ManagementScope scope = new ManagementScope(
                "\\\\servername\\root\\cimv2",
                options);
            try {
                scope.Connect();
                ManagementObject disk = new ManagementObject(
                    scope,
                    new ManagementPath("Win32_logicaldisk='c:'"),
                    null);
                disk.Get();
            }
            catch (Exception e) {
                Console.WriteLine("Failed to connect: " + e.Message);
            }
      

  3.   

    string strLoginID = "administrator"
    string strPassWord = "123"
    //Connect 
     ConnectionOptions options = new ConnectionOptions();
    options.Username = strLoginID;
    options.Password = strPassWord;
     ManagementScope scope = new ManagementScope(@"\\"+ strServerName +@"\root\cimv2", options);
     try
    {
    if (!scope.IsConnected)
     {
                scope.Connect();
       }
    }
    catch
    {}