C# 程序中如何本地打开远程共享文件(如mp3等),访问远程计算机需要用户名和密码(已知),也就是说,不需要弹出登陆框,用户名和密码自动完成输入并登陆打开所需文件,请高手指教!

解决方案 »

  1.   

    1.api函数
    WNetConnectionDialog 启动一个标准对话框,以便建立同网络资源的连接 
    WNetEnumResource 枚举网络资源 
    2.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();   
                      }   
                      catch   (Exception   e)   {   
                              Console.WriteLine("Failed   to   connect:   "   +   e.Message);   
                      }   
                      return   0;   
              }   
      }   
      

  2.   


    下面是例程:
    using System;
    using System.Management;
    using System.Text;
    using System.IO;class Sample_ConnectionOptions
    {
        public static void Main(string[] args)
        {        ConnectionOptions con = new ConnectionOptions();
            con.Username = "administrator";
            con.Password = "13579";
            string stringHostName = "192.168.0.8";        ManagementScope ms = new ManagementScope("\\\\" + stringHostName + "\\root\\cimv2", con); //\\root\\cimv2为脚本程序的默认名称空间
            ObjectQuery oq = new ObjectQuery("SELECT  *  FROM  Win32_OperatingSystem ");
            ManagementObjectSearcher query = new ManagementObjectSearcher(ms, oq);
            ManagementObjectCollection queryCollection = query.Get();        //列出服务器所有信息
            foreach (ManagementObject mo in queryCollection)
            {
                Console.WriteLine("Operating   System:   " + mo["Caption"]);
                Console.WriteLine("Version:   " + mo["Version"]);
                Console.WriteLine("Manufacturer   :   " + mo["Manufacturer"]);
                Console.WriteLine("Computer   Name   :   " + mo["csname"]);
                Console.WriteLine("Windows   Directory   :   " + mo["WindowsDirectory"]);        }
            //下面开始访问192.168.0.8机器e盘下所有文件夹
            DirectoryInfo di = new DirectoryInfo(@"\\192.168.0.8\e$");
            StringBuilder sb = new StringBuilder();
            foreach (DirectoryInfo subDIR in di.GetDirectories())
            {
                sb.Append(subDIR.Name + "\n\r");
            }
            Console.WriteLine("OK\n\r" + sb.ToString());       
        }
    }输出:
    Operating   System:   Microsoft(R) Windows(R) Server 2003, Enterprise Edition
    Version:   5.2.3790
    Manufacturer   :   Microsoft Corporation
    Computer   Name   :   SOP-SERVER
    Windows   Directory   :   C:\WINDOWS
    OK
    abc
    RECYCLER
    release
    SOP_VSS
    SOP_WS
    System Volume Information
    跟单系统请按任意键继续. . .
      

  3.   

    既然知道用户名密码,用NET USE吧
    http://blog.csdn.net/lovefootball/archive/2008/08/25/2827139.aspx