要实现一个CS结构的文件管理系统,项目要求Vs2005 C# 2.0 开发,winForm 窗体模式,客户端和Ftp服务器都放在局域网内;
我用一个TreeView遍历到服务器的所有文件夹,以树状形式存放到TreeView中,然后选中节点后自动添加到右边的
一个ListView中,如下图:
在开发的过程中有几个问题不能解决,希望大家帮忙指教:1、我现在之是遍历到本地磁盘的文件,怎么连接到服务器遍历到服务器的。
2、怎么获取选中的TreeView节点下文件的大小、并添加到ListView中。
3、能给点源代码或者项目简单实例更好,希望大侠们帮帮忙。感激不尽...!

解决方案 »

  1.   

    获取选择的节点,遍历子节点
    根据共享目录
    DirectoryEntry root = new DirectoryEntry("WinNT:");  
      DirectoryEntries domains = root.Children;  
      domains.SchemaFilter.Add("domain");  
      foreach (DirectoryEntry domain in domains) {  
      IPHostEntry iphe = null;  
      try {  
      iphe = Dns.GetHostByName(computer.Name);  
      }  
      catch { }  
      }foreach(TreeNode node in node.ChildNodes){}
    http://www.codeproject.com/KB/miscctrl/FileBrowser.aspx
      

  2.   

    to #1楼:遍历到本地磁盘的文件没问题;主要是怎么去遍历Ftp服务器的文件夹!
      

  3.   

    先去找个Ftp的Client,例如:
    http://www.codeproject.com/KB/IP/FtpClient.aspx
    剩下的自己写啦.
      

  4.   

    .net FtpWebRequest 实现FTP常用功能 
      

  5.   

    2、怎么获取选中的TreeView节点下文件的大小、并添加到ListView中。
      利用file 的size 
     
      

  6.   

    最好的办法不是将服务器纳入共享资源中,而是应该使用Ftp+Http协议来获取服务器目录
    比如,你发送一个获取服务器中某个文件夹内内容的请求,服务器端接收请求后将文件夹内文件内容全部拼凑成字符串,再发送回客户端,客户端收到应答后,解析字符串内容,再进行处理就OK了
    这个就是一般FTP服务端和客户端的交流方式。
      

  7.   

    LZ你去看看吧
    http://www.codeproject.com/KB/IP/win7ftp.aspx
    有源码
      

  8.   

     承蒙各位的帮助,有些功能已解决,但还存在如下几个难点:
     
     难点1、删除文件夹时,如果文件夹内有子文件或子文件夹,程序报错?
     难点2、怎么查看文件内容,如下图的 文本文件、word文件、excel文件、图片文件?
     难点3、批量的文件上传或下载;或者直接拖放,可多选? 
      

  9.   

    补充下:
    难点2、怎么直接查看Ftp服务器上的文件内容,如下图的 文本文件.txtword文件.docexcel文件.xls图片文件.jpg?
      

  10.   


    是在Ftp服务器上实现呀,不是本地目录!
      

  11.   

    难点1、删除文件夹时,如果文件夹内有子文件或子文件夹,程序报错?
    循环递归,挨个删除,例如
    private void DoDeleteDirs(string _Path, FTPclient _FTPclient)
            {
                FTPdirectory _FTPdirectory = _FTPclient.ListDirectoryDetail(_Path);
                foreach (FTPfileInfo file in _FTPdirectory)
                {
                    if (string.IsNullOrEmpty(file.Filename) || file.Filename.Equals(".") || file.Filename.Equals(".."))
                    {
                        continue;
                    }
                    if (file.FileType == FTPfileInfo.DirectoryEntryTypes.Directory)
                    {
                        if (!_FTPclient.FtpDeleteDirectory(_Path + "/" + file.Filename))
                        {
                            DoDeleteDirs(_Path + "/" + file.Filename, _FTPclient);
                            _FTPclient.FtpDeleteDirectory(_Path + "/" + file.Filename);
                        }
                    }
                }
            }        private void DoDeleteFiles(string _Path, FTPclient _FTPclient, bool _IsFile)
            {            FTPdirectory _FTPdirectory = _FTPclient.ListDirectoryDetail(_Path);
                foreach (FTPfileInfo file in _FTPdirectory)
                {
                    if (string.IsNullOrEmpty(file.Filename) || file.Filename.Equals(".") || file.Filename.Equals(".."))
                    {
                        continue;
                    }
                    if (file.FileType == FTPfileInfo.DirectoryEntryTypes.File)
                    {
                        if (_IsFile)
                            _FTPclient.FtpDelete(_Path);
                        else
                            _FTPclient.FtpDelete(_Path + "/" + file.Filename);
                    }
                    else
                    {
                        DoDeleteFiles(_Path + "/" + file.Filename, _FTPclient, false); ;
                    }
                }
            }
     难点2、怎么查看文件内容,如下图的 文本文件、word文件、excel文件、图片文件?
     你的FtpClient可以从Server得到文件的类型,你自己再根据类型选择图片并显示.
     难点3、批量的文件上传或下载;或者直接拖放,可多选?
    拖放需要设置控件的AllowDrop属性为true
    然后写DragEnter和DragDrop事件例如
    private void DoDragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            }private void DoDragDrop(object sender, DragEventArgs e)
            {
                //得到拖入的文件路径,可以是多个路径
                string[] _Files = (string[])e.Data.GetData(DataFormats.FileDrop);
                //判断拖入的Path中是否包含有文件夹,如果有,依次递归遍历得到所有的文件路径,空文件夹则直接忽略
                 //最后将处理过的文件路径依次上传. 
            }
      

  12.   

    难点2、怎么查看文件内容,如下图的 文本文件、word文件、excel文件、图片文件?
     如需查看文件内容,只能下载下来,再自动打开查看
      

  13.   

    用Upload()方法上传大一点的附件时会出现如下报错:
    求解!
      

  14.   

    Upload()的源码如下:public void Upload(string filename,string strPath)
            {
                FileInfo fileInf = new FileInfo(filename);
                string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
                FtpWebRequest reqFTP;            // Create FtpWebRequest object from the Uri provided
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strPath + "/" + fileInf.Name));            // Provide the WebPermission Credintials
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);            // By default KeepAlive is true, where the control connection is not closed
                // after a command is executed.
                reqFTP.KeepAlive = true;            // Specify the command to be executed.
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;            // Specify the data transfer type.
                reqFTP.UseBinary = true;            // Notify the server about the size of the uploaded file
                reqFTP.ContentLength = fileInf.Length;            // The buffer size is set to 2kb
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;            // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
                FileStream fs = fileInf.OpenRead();            try
                {
                    // Stream to which the file to be upload is written
                    Stream strm = reqFTP.GetRequestStream();                // Read from the file stream 2kb at a time
                    contentLen = fs.Read(buff, 0, buffLength);                // Till Stream content ends
                    while (contentLen != 0)
                    {
                        // Write Content from the file stream to the FTP Upload Stream
                        strm.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffLength);
                    }                // Close the file stream and the Request Stream
                    strm.Close();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Upload Error");
                }
            }
      

  15.   

    您好,这个例子您还有吗?有的话麻烦您发我一份,谢谢! [email protected]
      

  16.   

    楼主您好,这个例子您还有吗?我也在卡在您所说的地方了,能给我发一份例子吗,谢谢! [email protected]