我想问在服务中如何实现访问局域网中的共享目录,知道帐户,密码,(可以映射网络磁盘)。好像直接用io操作会报错.

解决方案 »

  1.   

    我是先进一次要访问的主机。然后把用户名和密码输入,选中保存密码。
    再用Systm.IO就可以了。
    //192.168.0.98/$D
      

  2.   

    先使用Wmi开通连接,再Copy文件
    下面是sdk中的例子。
    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;
        }
    }
      

  3.   

    有没有具体的例子啊,我是要在windows服务中实现,直接用i/o操作好像不行。先谢谢了!
      

  4.   

    http://community.csdn.net/Expert/topic/4048/4048955.xml?temp=.4025995
    或者使用net use命令: Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start();  string command = "net use " + remote + " " + pass + " /user:" + user; p.StandardInput.WriteLine(command); p.WaitForExit(1000); p.StandardInput.WriteLine("exit"); p.Dispose();
      

  5.   

    我写的服务中程序找不到映射的盘,但是在另外一个程序(winform)中是可以的,不知道为什么啊?