情况如下:我现在在做一个项目时要实现一个文件传输功能,其中要给另外一台计算机传输文件目录,不过接受方(服务器)是被动的,所以我在发送文件之前要选择文件存放的路径,不过怎么获得对方计算机的文件目录结构我不知道该怎么办
我想是把服务器的文件目录信息使用树形结构显示出来,让用户选择
请大家指个方向和思路,该怎么做,或者怎么搜相关的资料,我不知道该怎么形容这个事情,呵呵,搜了半天没搜到什么东西

解决方案 »

  1.   

    Socket如果要客户端选择目录,那么就把选择的目录先传给服务器.服务器判断,有没有该目录,没有就创建是否创建成功----发生一个结果给client,然后在发送文件-------------------------得到文件目录,不知道有什么直接方法没有.........但是 可以用FileInfo  和 DirectoryInfo 把文件夹和文件 循环遍历出来.....发送给客户单
      

  2.   

    嗯,遍历本地文件夹得话貌似问题还不大,我今天试了一下,关键是怎么遍历对方机器上的文件夹呢,更关键的是我遍历之后是个FileInfo对象和DirectoryInfo的对象,或者是个数组,我怎么传给客户机呢,我总不能写到文件里传过来再读吧,感觉太绕路了
      

  3.   

    这个需要映射磁盘。可以通过 Telent或者IPC$,登录对方的操作系统。
      

  4.   

    转自MSCN,供参考,下载文件
    using System;
    using System.IO;
    using System.Net;
    using System.Text;namespace Examples.System.Net
    {
        public class WebRequestGetExample
        {
            public static void Main ()
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
                request.Method = WebRequestMethods.Ftp.DownloadFile;            // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential ("anonymous","[email protected]");            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                Console.WriteLine(reader.ReadToEnd());            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
        
                reader.Close();
                response.Close();  
            }
        }
    }
      

  5.   

    转自MSND,供参考,上载文件
    using System;
    using System.IO;
    using System.Net;
    using System.Text;namespace Examples.System.Net
    {
        public class WebRequestGetExample
        {
            public static void Main ()
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
                request.Method = WebRequestMethods.Ftp.UploadFile;            // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential ("anonymous","[email protected]");
                
                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader("testfile.txt");
                byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;            Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        
                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
        
                response.Close();
                }
            }
        }
    }
      

  6.   

    转自MSDN,供参考,获取服务器目录
    using System;
    using System.IO;
    using System.Net;
    using System.Text;namespace Examples.System.Net
    {
        public class WebRequestGetExample
        {
            public static void Main ()
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
                request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;            // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential ("anonymous","[email protected]");            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                Console.WriteLine(reader.ReadToEnd());            Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
        
                reader.Close();
                response.Close();
            }
        }
    }
      

  7.   

    嗯,这个....
    可能我没说清楚,我要C/S模式的,并且我也没有架设FTP,我不可能再为了获取个目录再做个FTP吧
    我再考虑下别的方式
      

  8.   

    自己写一个WebService/windows Service返回指定目录的文件目录
      

  9.   

    我使用TcpClient与TcpListener基本实现功能了